C Operators

C operators are used in programs to trigger an action i.e. to perform certain mathematical or logical manipulations.

C operators are classified as follows:

Arithmetic Operators

Arithmetic operators available in c are as follows:

Arithmetic Operator Symbol Description Example
+ Performs addition 1+1=2
Performs subtraction 1-1=0
* Performs multiplication 2*2=4
/ Performs division 10/2=5
% Performs modular division(Gives remainder after performing division) 11%2=1
+(Unary Plus) Gives value of operand  +5=5
-(Unary Minus) Changes sign of its operand -5

Relational Operators

All relational operators gives answer in either TRUE or FALSE. Every non zero value is consider as TRUE by the compiler.

Relational operators available in c are as follows:

Relational Operator Symbol Meaning Example
== Comparison(equality) 2==2 (gives TRUE)
> Greater than 5>6 (gives FALSE)
>= Greater than or equal to 5>=5 (gives TRUE)
< Less than 5
<= Less than or equal to 2
!= Not equal to 5!=2 (gives TRUE)

Logical Operators

Unlike relational operators that establish relationship among values, logical operators refers to the ways these relationships among values can be connected.

There are three logical operators in c:

Logical Operator Symbol Meaning Example Result
&& AND (If anyone condition is False, answer is False) 6>=0 && 6>=20 False
|| OR (If anyone condition is True, answer is True) 2>=1 || 1>=5 True
! NOT (It negates value of its operand and is an unary operator) !(6<=6) False

Assignment Operators

Assignment operators are used to assign the result of an expression or a particular value to a variable.

The symbol of assignment operator is “=”.

For example, a=11 will assign value 11 directly in a, and x=a+b will assign value of expression a+b in x.

C also has a set of shorthand assignment operators. Consider this statement,

a=a+b;

The above statement can also be written using shorthand assignment operators as

a+=b;

Statement with simple assignment operator Statement with shorthand assignment operator
a=a+1; a+=1;
a=a-1; a-=1;
a=a*(b+c); a*=b+c;
a=a/(b+c); a/=b+c;
a=a%b; a%=b;

Increment & Decrement Operators

C also provides the unary increment operator(++) and the unary decrement operator(–).

The increment operator adds 1 to its operand whereas the decrement operator subtracts 1 from its operand.

We can use increment and decrement operators in anyone of the following form:

Postfix: If increment and decrement operators are placed after a variable, they are said to be postfix increment or decrement operators.

Postincrementing or postdecrementing the variable causes current value to be used in expression in which it appears, then variable value is incremented or decremented.

Prefix: If increment and decrement operators are placed before a variable, they are said to be prefix increment or decrement operators.

Preincrementing or predecrementing the variable causes current value to be incremented or decremented first, and then only the variable value is used in the expression.

The rules can be summarized as:

  1. Prefix: Change then use
  2. Postfix: Use then change

Conditional Operators

The only ternary operator available in c is “?:” to costruct conditional expression.

Syntax

expression_1?expression_2:expression_3;

The expression_1 is a logical or relational condition which is evaluated first. If the evaluated value is TRUE, then the value of complete expression will be expression_2 otherwise, it will be expression_3.

For example,

large=(a

Special Operators

Special operators available in c are as follows:

Comma Operator(,)

The comma operator is used to link the related expression together. Since, comma operator has the lowest precedence over other C operators, therefore it is always given in brackets.

It is evaluated from left to right, and the evaluated rightmost value is the value of combined expression.

For example,

value=(a=10,a++,a*2);

In the above example, first 10 is assigned to a, then a is incremented by 1, and finally 11*2 is assigned to a.

Sizeof Operator

The sizeof operator is a compile time operator which returns the number of bytes that the operand occupies. The operand may be a variable, constant or a data type.

Syntax

sizeof var;
or
sizeof (type);

where var is a declared variable and type is a valid c data type.

For example,

a=sizeof(sum);
b=sizeof(long int);
c=sizeof sum;

Bitwise Operators

There are also six bitwise operators available in c:

  1. Bitwise AND Operator (&)
  2. Bitwise OR Operator (|)
  3. Bitwise Exclusive OR Operator (^)
  4. Ones Complement Operator (~)
  5. Left Shift Operator (<>)
  6. Right Shift Operator (>>)

Please comment with your real name using good manners.

Leave a Reply