Operators in C Language
Operators in C Language
or in C
Arithmetic Operators
Relational Operators
Langua
Logical Operators
Bitwise Operators
ge
Assignment Operators
Misc Operators
Arithmetic The following table shows all the arithmetic operators supported by the C
language. Assume variable var2 holds 10 and variable var2 holds 20 then:
Operators:
Operators Description Example
== Checks if the values of two operands are equal or not. If yes, then the ( var1 == var2 ) is not true.
condition becomes true.
!= Checks if the values of two operands are equal or not. If the values are not ( var1 != var2 ) is true.
equal, then the condition becomes true.
> Checks if the value of left operand is greater than the value of right operand. ( var1 > var2 ) is not true.
If yes, then the condition becomes true.
< Checks if the value of left operand is less than the value of right operand. If ( var1 < var2 ) is true.
yes, then the condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value of ( var1 >= var2 ) is not true.
right operand. If yes, then the condition becomes true.
<= Checks if the value of left operand is less than or equal to the value of right ( var1 <= var2 ) is true.
operand. If yes, then the condition becomes true.
Operators:
Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the condition
( var1 && var2 )
becomes true. is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition ( var1 || var2 ) is
becomes true. true.
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a !( var1 && var2 )
condition is true, then Logical NOT operator will make it false. is true.
Bitwise operator works on bits and perform bit-by-bit operation. The truth
Bitwise tables for &, |, and ^ is as follows:
Operators:
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
The following table lists the bitwise operators in C. Assume variable 'var1' holds 60 and variable ' var2' holds
13, then:
& Binary AND Operator copies a bit to the result if it exists in both (var1& B) = 12, i.e., 0000 1100
operands.
| Binary OR Operator copies a bit if it exists in either operand. (var1 | var2) = 61, i.e., 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not (var1 ^ var2) = 49, i.e., 0011 0001
both.
~ Binary One's Complement Operator is unary and has the effect of (~var1) = ~(60), i.e,. -0111101
'flipping' bits.
<< Binary Left Shift Operator. The left operands value is moved left by
var1 << 2 = 240 i.e., 1111 0000
the number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operands value is moved right
var1 >> 2 = 15 i.e., 0000 1111
by the number of bits specified by the right operand.
= Simple assignment operator. Assigns values from right side C = A + B will assign the value of A +
operands to left side operand B to C
%= Modulus AND assignment operator. It takes modulus using var3 %= var1 is equivalent to var3 =
two operands and assigns the result to the left operand. var3 % var1
<<= Left shift AND assignment operator. var3 <<= 2 is same as var3 = var3 << 2
>>= Right shift AND assignment operator. var3 >>= 2 is same as var3 = var3 >> 2
&= Bitwise AND assignment operator. var3 &= 2 is same as var3 = var3 & 2
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an expression is
evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a
higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so
it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the
bottom. Within an expression, higher precedence operators will be evaluated first.
Show Examples