Operat
Operators is c language is a symbol that tells the compiler to perform specific
mathematical or logical tasks. C language is rich in built-in operators. C
language provides the following types of operators:
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
+ Adds two operands. var1 + var2 = 30
− Subtracts second operand from the first. var1 − var2 = -10
* Multiplies both operands. var1 * var2 = 200
/ Divides numerator by de-numerator. var2 / var1 = 2
% Modulus Operator and remainder of after an integer division. var2 % var1 = 0
++ Increment operator increases the integer value by one. var1 ++ = 11
-- Decrement operator decreases the integer value by one. var1 -- = 9
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.
The following table shows all the
Relational Operators: relational operators in C. Assume
variable var1 holds 10 and
variable var2 holds 20 then:
Following table shows all the logical operators in C language. Assume
Logical variable var1 holds 1 and variable var2 holds 0, then:
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
Assume var1 = 60 and var2 = 13 in binary format, they will be as follows:
var1 = 0011 1100
var1 = 0000 1101
-----------------
var1 & var2 = 0000 1100
var1 | var2 = 0011 1101
var1 ^ var2 = 0011 0001
~ var1 = 1100 0011
The following table lists the bitwise operators in C. Assume variable 'var1' holds 60 and variable ' var2' holds
13, then:
Operator Description Example
& 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.
The following table lists the assignment operators in the C language:
Assignment
Operators:
Operator Description Example
= 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
+= Add AND assignment operator. It adds the right operand to the
C += A is equivalent to C = C + A
left operand and assign the result to the left operand.
-= Subtract AND assignment operator. It subtracts the right
operand from the left operand and assigns the result to the left C -= A is equivalent to C = C - A
operand.
*= Multiply AND assignment operator. It multiplies the right
operand with the left operand and assigns the result to the left C *= A is equivalent to C = C * A
operand.
/= Divide AND assignment operator. It divides the left operand
var3 /= var1 is equivalent to var3 =
with the right operand and assigns the result to the left
var3 / var1
operand.
%= 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
^= Bitwise exclusive OR and assignment operator. var3 ^= 2 is same as var3 = var3 ^ 2
|= Bitwise inclusive OR and assignment operator. var3 |= 2 is same as var3 = var3 | 2
Besides the operators discussed above, there are a few other important operators
Misc Operators ↦ including sizeof and ? : in the C Language:
sizeof & ternary
Operator Description Example
sizeof() sizeof(var1), where var1 is int, will
Returns the size of a variable.
return 4.
& &var1; returns the actual address of
Returns the address of a variable.
the variable.
* Pointer to a variable. *var1;
?: If Condition is true ? then value exp1 :
Conditional Expression.
otherwise value exp2
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
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right