Operators

Operators are used to perform operations. Operators are the symbols which perform the operation on the some values. These values are known as operands. There are following types of operators to perform different types of operations in C language:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Misc Operators

 
1. Arithmetic Operators

OperatorOperator NameDescriptionExample
+AdditionAdds two operandsI = 40, J= 20I + J = 60
SubtractionSubtracts second operand from the firstI = 40, J= 20I – J = 20
*MultiplicationMultiplies both operandsI = 40, J= 20I * J = 800
/DividePerform division operationI = 40, J= 20I / J = 2
%ModulusReturn the remainder after DivisionI = 40, J= 20I % J = 0
++IncrementIncrease the operand value by 1I=40,I++ = 41, ++I  =  40 (print 40 but next time its value is 41)
DecrementDecrease the operand value by 1 I=40I– = 39, –I =  40
(print 40 but next time its value is 39)

2. Relational Operators
It is also known as comparison operator because it compares the values. After comparison it returns the Boolean value i.e. either true or false.

OperatorOperator NameDescriptionExample
==Equal toIf the values of two operands are equal then it returns true.I = 20, J =20(I == J) is true
!= Not Equal toIf the values of two operands are not equal then it returns true.I = 20, J =20(I == J) is False
<Less thanIf the value of left operand is less than the value of right operand then it returns trueI = 40, J =20(I < J) is False
>Greater thanIf the value of left operand is greater than the value of right operand then it returns trueI = 40, J =20(I > J) is True
<=Less than or equal toIf the value of left operand is less than or equal to the value of right operand then it returns true.I = 40, J =20(I <= J) is False
>=Greater than or equal toIf the value of left operand is greater than or equal to the value of right operand then it returns true.I = 40, J =20(I >= J) is True

3. Logical Operators

OperatorOperator NameDescriptionExample
andLogical ANDWhen Both side condition is true the result is true otherwise false2<1 and 2<3False
orLogical ORWhen at least one condition is true then result is true otherwise false2<1 or 2<3True
notLogical NOTReverse the conditionNot(5>4)False

4. Bitwise Operators
It performs bit by bit operation. Suppose there are two variable I = 10 and J = 20 and their binary values are
I = 10 = 0000 1010
J = 20 = 0001 0100

OperatorOperator NameDescriptionExample
&Binary ANDIf both bits are 1 then 1 otherwise 0 I & J0000 0000
|Binary ORIf one of the bit is 1 then 1 otherwise 0I | J0001 1110
^Binary XORIf both bit are same then 0 otherwise 1I ^ J0001 1110
~Binary ComplementIf bit is 1 the make it 0 and if bit is 0 the make it 1~I1111 0101
<<Binary Left ShiftThe left operand is moved left by the number of bits specified by the right operand.I << 2 will give 240 i.e. 1111 0000
>>Binary Right ShiftThe left operand is moved right by the number of bits specified by the right operand.I >> 2 will give 15 i.e. 1111

5. Assignment Operators

OperatorOperator NameDescriptionExample
=AssignmentIt assigns value from right side operand to left side operandI = 40It assigns 40 to I
+=Add then assignIt performs addition and then result is assigned to left hand operand I+=Jthat means
I = I + J
-=Subtract then assignIt performs subtraction and then result is assigned to left hand operand I-=Jthat means
I = I – J
*=Multiply the assignIt performs multiplication and then result is assigned to left hand operand. I*=Jthat means
I = I * J
/=Divide then assignIt performs division and then result is assigned to left hand operand I/=Jthat means
I = I / J
%=Modulus then assignIt performs modulus and then result is assigned to left hand operand I%=Jthat means
I = I % J
<<=Left shift AND assignment operatorIt performs Binary left shift and then result is assigned to left hand operandI<<=5that means
I = I << 5
>>=Right shift AND assignment operatorIt performs Binary right shift and then result is assigned to left hand operandI>>=5that means
I = I >>=5
&=Bitwise AND assignment operatorIt performs bitwise AND and then result is assigned to left hand operand I &= 5that means
I = I & 5
^=bitwise exclusive OR and assignment operatorIt performs bitwise exclusive OR and then result is assigned to left hand operand I ^= 5that means
I = I ^ 5
|=bitwise inclusive OR and assignment operatorIt performs bitwise inclusive OR and then result is assigned to left hand operand I |= 5that means
I = I | 5

6. Misc Operators 
There are few other important operators including sizeof and ? : supported by C Language.

OperatorDescription
sizeof()Returns the size of an variable.
&Returns the address of an variable.
*Pointer to a variable.
? :Conditional Expression

Operators Precedence in C

Category Operator Associativity 
Postfix() [] -> . ++ – –Left to right
Unary+ – ! ~ ++ – – (type)* & sizeofRight 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

Recommended Videos

Leave a Reply

Your email address will not be published. Required fields are marked *