Operators and Expressions:
An operator is a symbol that operates on a value or a variable. For
example: + is an operator to perform addition.
C has a wide range of operators to perform various operations.
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment and Decrement Operators
5. Assignment Operators
6. Bitwise Operators
7. Other Operators
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as
addition, subtraction, multiplication, division etc on numerical values
(constants and variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* Multiplication
/ Division
% remainder after division (modulo division)
Example: 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;
C Relational Operators
A relational operator checks the relationship between two operands.
If the relation is true, it returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
Operator Meaning of Operator Example
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
Example: Relational Operators
// Working of relational operators
#include <stdio.h>
int main()
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Logical Operators
An expression containing logical operator returns either 0 or
1 depending upon whether expression results true or false.
Logical operators are commonly used in decision making in C
programming.
Operator Meaning Example
Logical AND. True only if all operands If c = 5 and d = 2 then, expression ((c==5) &&
&&
are true (d>5)) equals to 0.
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5) ||
||
operand is true (d>5)) equals to 1.
Logical NOT. True only if the operand is
! If c = 5 then, expression !(c==5) equals to 0.
0
Example:
#include <stdio.h>
int main()
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
Increment and Decrement Operators
C programming has two operators increment ++ and decrement -- to change the
value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value
by 1. These two operators are unary operators, meaning they only operate on a
single operand.
Increment and Decrement Operators
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Assignment Operators
An assignment operator is used for assigning a value to a variable. The
most common assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example : 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;
C Bitwise Operators
During computation, mathematical operations like: addition,
subtraction, multiplication, division, etc are converted to bit-level which
makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level
operations.
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
Operators Meaning of operators
>> Shift right
Bitwise AND Operator &
The output of bitwise AND is 1 if the corresponding bits of two
operands is 1. If either bit of an operand is 0, the result of corresponding bit
is evaluated to 0.
In C Programming, the bitwise AND operator is denoted by & .
Let us suppose the bitwise AND operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001000
& 00011001
00001000 = 8 (In decimal)
Example : Bitwise AND
#include <stdio.h>
int main()
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
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 |.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)
Example 2: Bitwise OR
#include <stdio.h>
int main()
int a = 12, b = 25;
printf("Output = %d", a | b);
return 0;
Bitwise XOR (exclusive OR) Operator ^
The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is
denoted by ^.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101 = 21 (In decimal)
Example 3: Bitwise XOR
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a ^ b);
return 0;
Bitwise Complement Operator ~
Bitwise complement operator is a unary operator (works on only one operand). It
changes 1 to 0 and 0 to 1. It is denoted by ~.
35 = 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
________
11011100 = 220 (In decimal)
12 00001100 -(11110011+1) = -11110100 = -244(decimal)
220 11011100 -(00100011+1) = -00100100 = -36(decimal)
Example 4: Bitwise complement
#include <stdio.h>
int main()
printf("Output = %d\n", ~35);
printf("Output = %d\n", ~-12);
return 0;
Shift Operators in C programming
There are two shift operators in C programming:
Right shift operator
Left shift operator.
Right Shift Operator
Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted
by >>.
212 = 11010100 (In binary)
212 >> 2 = 00110101 (In binary) [Right shift by two bits]
212 >> 7 = 00000001 (In binary)
212 >> 8 = 00000000
212 >> 0 = 11010100 (No Shift)
Left Shift Operator
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 <<.
212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 = 11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)
Example #5: Shift Operators
#include <stdio.h>
int main()
int num=212, i;
for (i = 0; i <= 2; ++i)
printf("Right shift by %d: %d\n", i, num >> i);
printf("\n");
for (i = 0; i <= 2; ++i)
printf("Left shift by %d: %d\n", i, num << i);
return 0;
Other Operators
Comma Operator
Comma operators are used to link related expressions together.
For example: int a, c = 5, d;
The size of operator
The size of is a unary operator that returns the size of data
(constants, variables, array, structure, etc).
Example: size of Operator
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}