C Programming language-Operators in C

Operators in C

An Operator is a symbol that instructs the compiler to perform certain mathematical or logical functions. The following are the Operators present in C language,
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators
Arithmetic Operators

Arithmetic Operators perform common mathematical operations in C programming language. For example ‘a’ holds value 10 and ‘b’ holds value 5, then

Example

Operators
Description
Example
+
Adds two operands
a+b = 15
-
Subtracts second from the first operand
a-b = 5
*
Multiplies both the operands
a*b =  50
/
Divides numerator by denominator
a / b = 2
%
Remainder  of an integer division
a % b = 0
++
Increment the value by 1
a++ = 11
--
Decrement the value by 1
a-- = 9
Relational Operators

The following are the relational operators which are supported in C language. For example, the integer ‘a’ holds value 10 and ‘b’ holds value 5, then

Example

Operator 
Description
Example
==
Returns true only if the value of two operands are equal
(a == b) is not true
!=
Returns true only if the value of two operands are not equal
(a != b) is true
>
Returns true if the value of left operand is greater than right operand
(a > b) is true
<
Returns true if the value of left operand is lesser than right operand
(a < b) is not true
>=
Returns true if the value of left operand is greater than or equal to the right operand
(a >= b) is true
<=
Returns true if the value of left operand is lesser than or equal to the right operand
(a <= b) is not true
Logical Operators

Logical Operators are used to determine the logic between the variables and values. For example, the variable ‘A’ holds 1 and the variable ‘B’ holds 0 :

Operator
Name
Description
Example
||
Logical OR
Returns true if one of the statement is true
A || B is TRUE
&&
Logical AND
Returns true if both the statements are true
A && B is FALSE
!
Logical NOT
Returns true if the statement is false (Reverse the result)
!(A || B) is FALSE
Bitwise Operator

Bitwise Operator performs bit by bit operation. The following table gives you the results of bitwise operators such as |, & and ^

Example

p
q
p | q
p & q
p ^ q
00000
01101
10101
11110
From the above table,
| - Bitwise OR, TRUE if any one of the bit is TRUE
& - Bitwise AND, TRUE only if both the bits are TRUE
^ - Bitwise XOR, TRUE only if only one bit is TRUE
Let assume, the variable ‘A’ holds the value 60 and the variable ‘B’ holds the value 13, then
A = 60 => 0011 1100
B = 13 => 0000 1101

Operator
Name
Description
Example
|
Logical OR
Returns 1 if any one of the bit is 1
A|B = 61 => 0011 1101
&
Logical AND
Returns 1 if only both the bits are 1
A & B = 12 = > 0000 1100
^
Logical XOR
Returns 1 if only either of the bit is 1
A ^ B = 49 => 0011 0001
~
One’s Complement Operator
This is a unary operator and has the effect of ‘flipping bits’
(~A) = ~(60) => -1100 0011 = -195
<<
Left Shift Operator
The left operand value is shifted left by the number of bits specified by the right operand 
A  <<  2 = 1111 0000 = 240
>>
Right Shift Operator
The left operand value is shifted right by the number of bits specified by the right operand
A >> 2 = 0000 1111 = 15
Assignment Operator

The Assignment Operator in C are given below :-

Operator
Description
Example
=
This is a Assignment Operator which assigns the values from right operand to the left operand
C = A + B, the value of A+B is assigned to the left operand C
+=
This adds the right operand to the left operand and assigns the value to the left operand 
B += A, the value of B+A is assigned to the left operand B
-=
This subtracts the right operand from the left operand and assigns the value to the left operand
B -= A, the value of B-A  is assigned to the left operand B
*=
This multiplies the right operand to the left operand and assigns the value to the left operand
B *= A, the value of B*A is assigned to the left operand B
/=
This divides the left operand with the right operand and assigns the value to the left operand
B /= A, the value of B/A is assigned to the left operand B
%=
This takes the modulus using two operands and assign the result to the left operand 
B %= A, the value of B%A is assigned to the left operand B
<<=
Left shift and assigned to the left operand
A<<=2, the value of A<<2 is assigned to the left operand A
>>=
Right shift and assigned to the right operand
A >>= 2, the value of A>>2 is assigned to the left operand A
&=
This performs bitwise AND and assigns the value to the right operand
A &= 2, the value of A&2 is assigned to the left operand A
|=
This performs bitwise Or and assigns the value to the right operand
A |= 2, the value of A|2 is assigned to the left operand A
^=
This performs bitwise XOR and assigns the value to the right operand
A ^= 2, the value of A^2 is assigned to the left operand A

Misc Operators

The Misc Operators are discussed here. There are below operators which are supported by C language:

Operator
Description
Example
sizeof()
Returns the size of a variable
sizeof(A); the size of the variable A  is 4
&
Returns the address of the variable
&A; returns the actual address of the variable
*
Pointer to a variable 
*A;
?:
Conditional Expression
X>Y?X:Y. If Condition is true? Then value X otherwise value Y
Operator Precedence in C

Operator Precedence determines the grouping of terms in an expression and decides in which order an expression is evaluated.
There are certain expressions which have higher precedence than other expressions. For example, the multiplication operator has
higher precedence compared to addition operator.

Example
x = 10 + 5 *2
The x is assigned 20, not 30 because the multiplication (*) operator has higher precedence compared to the addition (+) operator. Hence,
it first gets multiplied with 5*2 and then adds to 10.The following table shows the precedence order from higher to lower. The one
with the highest precedence present at the top and one with the lowest precedence present at the bottom.

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