C Programming Operators
C Programming 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
Increment and Decrement Operators
Misc Operators
1. Arithmetic Operators
Operator Operator Name Description Example
+ Addition Adds two operands I = 40, J= 20I + J =
60
– Subtraction Subtracts second operand from the I = 40, J= 20I – J =
first 20
* Multiplication Multiplies both operands I = 40, J= 20I * J =
800
/ Divide Perform division operation I = 40, J= 20I / J = 2
% Modulus Return the remainder after Division I = 40, J= 20I % J =
0
++ Increment Increase the operand value by 1 I=40,I++ =
41, ++I = 40 (print
40 but next time its
value is 41)
— Decrement Decrease the operand value by 1 I=40I– = 39, –I = 40
(print 40 but next
time its value is 39)
Example 1: Arithmetic Operators
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Run Code
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
a/b = 2.5
a/d = 2.5
c/b = 2.5
// Both operands are integers
c/d = 2
2. Relational Operators
return 0;
}
Run Code
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
3. Logical Operators
Operator Operator Description Example
Name
And Logical AND When Both side condition is true the result is 2<1 and
true otherwise false 2<3False
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Run Code
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
b) is 1 (true).
(a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
(a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
(a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c <
b) are 0 (false).
!(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a
!= b) is 1 (true).
!(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0
(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
Operator Operator Name Description Example
& Binary AND If both bits are 1 then 1 I & J0000 0000
otherwise 0
| Binary OR If one of the bit is 1 then 1 I | J0001 1110
otherwise 0
^ Binary XOR If both bit are same then 0 I ^ J0001 1110
otherwise 1
~ Binary Complement If bit is 1 the make it 0 and if bit ~I1111 0101
is 0 the make it 1
<< Binary Left Shift The left operand is moved left I << 2 will give
by the number of bits specified 240 i.e. 1111 0000
by the right operand.
>> Binary Right Shift The left operand is moved right I >> 2 will give 15
by the number of bits specified i.e. 1111
by the right operand.
int main() {
return 0;
}
Run Code
Output
Output = 8
Bitwise OR Operator |
The output of bitwise OR is 1 if at least one corresponding bit of two
operands is 1. In C Programming, bitwise OR operator is denoted by | .
Example 2: Bitwise OR
#include <stdio.h>
int main() {
return 0;
}
Run Code
Output
Output = 29
int main() {
return 0;
}
Run Code
Output
Output = 21
int main() {
return 0;
}
Output
Output = -36
Output = 11
Right shift operator shifts all bits towards right by certain number of
specified bits. It is denoted by >> .
Left shift operator shifts all bits towards left by a certain number of specified
bits. The bit positions that have been vacated by the left shift operator are
filled with 0. The symbol of the left shift operator is << .
int main() {
int num=212, i;
}
printf("\n");
return 0;
}
5. Assignment Operators
Operator Operator Name Description Example
= Assignment It assigns value from right side operand I = 40It assigns
to left side operand 40 to I
+= Add then assign It performs addition and then result is I+=Jthat means
assigned to left hand operand I=I+J
-= Subtract then It performs subtraction and then result is I-=Jthat means
assign assigned to left hand operand I=I–J
*= Multiply the It performs multiplication and then result I*=Jthat means
assign is assigned to left hand operand. I=I*J
/= Divide then It performs division and then result is I/=Jthat means
assign assigned to left hand operand I=I/J
%= Modulus then It performs modulus and then result is I%=Jthat means
assign assigned to left hand operand I=I%J
<<= Left shift AND It performs Binary left shift and then I<<=5that means
assignment result is assigned to left hand operand I = I << 5
operator
>>= Right shift AND It performs Binary right shift and then I>>=5that means
assignment result is assigned to left hand operand I = I >>=5
operator
&= Bitwise AND It performs bitwise AND and then result I &= 5that means
assignment is assigned to left hand operand I=I&5
operator
^= bitwise exclusive It performs bitwise exclusive OR and I ^= 5that means
OR and then result is assigned to left hand I=I^5
assignment operand
operator
|= bitwise inclusive It performs bitwise inclusive OR and then I |= 5that means
OR and result is assigned to left hand operand I=I|5
assignment
operator
Example 5: Assignment Operators
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Run Code
Output
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Here, the operators ++ and -- are used as prefixes. These two operators
can also be used as postfixes like a++ and a-- .
7. Misc Operators
There are few other important operators including sizeof and ? : supported by C
Language.
Operator Description
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)* & 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