Unit 2
OPERATORS
Every expression in C language contains set of operators and operands. Operators are the special symbols which are used to indicate the type of operation whereas operands are the data member or variables operating as per operation specified by operator. One expression contains one or more set of operators and operands. So to avoid the ambiguity in execution of expression, C compiler fixed the precedence of operator. Depending on the operation, operators are classified into following category.
 Arithmetic Operator: These are the operators which are useful in performing mathematical calculations. Following are the list of arithmetic operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5.
Sr. No.  Operator  Description  Example 
+  Used to add two operands  Result of A+B is 15  
2.    Used to subtract two operands  Result of AB is 5 
3.  *  Used to multiply two operands  Result of A*B is 50 
4.  /  Used to divide two operands  Result of A/B is 2 
5.  %  Find reminder of the division of two operand  Result of A%B is 0 
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c;
clrscr();
printf("Enter two numbers");
scanf("%d %d",&a,&b);
printf("\n Result of addition of %d and %d is %d",a,b,a+b);
printf("\n Result of subtraction of %d and %d is %d",a,b,ab);
printf("\n Result of multiplication %d and %d is %d",a,b,a*b);
printf("\n Result of division of %d and %d is %d",a,b,a/b);
printf("\n Result of modulus of %d and %d is %d",a,b,a%b);
getch();
}
Output:
Enter two numbers
5
3
Result of addition of 5 and 3 is 8
Result of subtraction of 5 and 3 is 2
Result of multiplication of 5 and 3 is 15
Result of division of 5 and 3 is 1
Result of modulus of 5 and 3 is 2
2. Relational Operators: Relational operator used to compare two operands. Relational operator produce result in terms of binary values. It returns 1 when the result is true and 0 when result is false. Following are the list of relational operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5.
Sr. No.  Operator  Description  Example 
1.  <  This is less than operator which is used to check whether the value of left operand is less than the value of right operand or not  Result of A<B is false

2.  >  This is greater than operator which is used to check whether the value of left operand is greater than the value of right operand or not  Result of A>B is true 
3.  <=  This is less than or equal to operator  Result of A<=B is false 
4.  >=  This is greater than or equal to operator  Result of A>=B is true 
5.  ==  This is equal to operator which is used to check value of both operands are equal or not  Result of A==B is false 
6.  !=  This is not equal to operator which is used to check value of both operands are equal or not  Result of A!=B is true 
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c;
clrscr();
printf("Enter two numbers");
scanf("%d %d",&a,&b);
printf("\n Result of less than operator of %d and %d is %d",a,b,a<b);
printf("\n Result of greater than operator of %d and %d is %d",a,b,a>b);
printf("\n Result of leass than or equal to operator %d and %d is %d",a,b,a<=b);
printf("\n Result of greater than or equal to operator of %d and %d is %d", a,b,a>=b);
printf("\n Result of double equal to operator of %d and %d is %d",a,b,a==b);
printf("\n Result of not equal to operator of %d and %d is %d",a,b,a!=b);
getch();
}
Output:
Enter two numbers
5
3
Result of less than operator of 5 and 3 is 0
Result of greater than operator of 5 and 3 is 1
Result of less than or equal to operator of 5 and 3 is 0
Result of greater than or equal to operator of 5 and 3 is 1
Result of double equal to operator of 5 and 3 is 0
Result of not equal to operator of 5 and 3 is 1
3. Logical Operators: Logical operators are used to compare logical values of two operands. Following are the list of logical operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5.
Sr. No.  Operator  Description  Example 
1.  &&  This is logical AND, it returns true when both the values are non zero  Result of (A&&B) is true 
2.    This is logical OR, it returns true when any of two value is non zero  Result of (AB) is true 
3.  !  This is logical NOT operator, it is used to reverse the logical state of operand  Result of !(A&&B) is false 
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c;
clrscr();
printf("Enter two numbers");
scanf("%d %d",&a,&b);
printf("\n Result of logical and operator of %d and %d is %d",a,b,a&&b);
printf("\n Result of logical or operator of %d and %d is %d",a,b,ab);
printf("\n Result of logical not operator of %d and and %d is %d",a,b,!(a&&b));
getch();
}
Output:
Enter two numbers
5
3
Result of logical and operator of 5 and 3 is 1
Result of logical or operator of 5 and 3 is 1
Result of logical not operator of 5 and 3 is 0
4. Assignment Operators: Assignment operator is used to assign the value to variable. To understand the operation assume variable A contains value 10 and variable contains value 5.
Example: A=B;
In the above example value of variable B is assign to variable A. Result of this expression is value of A is 5 and value of B is 5.
5. Increments and Decrement Operators: These operator are used to increase or decrease the value of variable by one. Following are the list of logical operator in C language. To understand the operation assume variable A contains value 10.
Sr. No.  Operator  Description  Example 
1.  ++  This is incremental operator which is used to increase the value of variable by 1  Result of (A++) is value of A is 11 
2.    This is decremental operator which is used to decrease the value of variable by 1  Result of (A) is value of A is 9 
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=5,c=5,d=5;
clrscr();
printf("Original value of a, b, c, d is %d \t %d \t %d \t %d",a,b,c,d);
a++;
printf("\n New value of a is %d",a);
++b;
printf("\n New value of b is %d",b);
printf("\n New value of c is %d",c++);
printf("\n New value of d is %d",++d);
getch();
}
Output:
Original value of a, b, c, d is 5 5 5 5
New value of a is 6
New value of b is 6
New value of c is 5
New value of d is 6
6. Bitwise Operators: These operators are used to work on the each bits of data. These operators can work on the binary data. If the data is non binary then computer first of all convert it into binary form and then perform the operation. Following are the list of logical operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5. So the binary values of
A = 0000 1010 and
B = 0000 0101
Sr. No.  Operator  Description  Example 
1.  &  This is bitwise AND  Result of (A&B) is 0000 0000 
2.    This is bitwise OR  Result of (AB) is 0000 1111 
3.  ^  This is bitwise Exclusive  Result of (A^B) is 0000 1111 
4.  <<  This is used to shift bit on left side  Result of (A<<B) is 
5.  >>  This is used to shift bit on right side  Result of (A>>B) is 
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c;
clrscr();
printf("Enter two numbers");
scanf("%d %d",&a,&b);
printf("\n Result of bitwise and operator of %d and %d is %d",a,b,a&b);
printf("\n Result of bitwise or operator of %d and %d is %d",a,b,ab);
printf("\n Result of bitwise exclusive operator of %d and %d is %d",a,b,a^b);
printf("\n Result of bitwise left shift operator of %d and %d is %d",a,b,a<<b);
printf("\n Result of bitwise right shift operator of %d and %d is %d",a,b,a>>b);
getch();
}
Output:
Enter two numbers
5
3
Result of bitwise and operator of 5 and 3 is 1
Result of bitwise or operator of 5 and 3 is 7
Result of bitwise exclusive operator of 5 and 3 is 6
Result of bitwise left shift operator of 5 and 3 is 40
Result of bitwise right shift operator of 5 and 3 is 0
Hierarchy/Precedence of operations
Expression may have one or more operators in it. If expression has multiple operators then these operators are solved according to hierarchy of operators. Hierarchy determines the order in which these operators are executed.
Table : Hierarchy of operators
Functions  Top Priority 
Power 

Mod 

*,/ 

+, 

=,<,>,<=,>= 

NOT 

AND 

OR  Least Priority 
Ex. 1
Find the result of the following operations.
Sr. No.  Operation 
1.  10/2 
2.  15 MOD 2 
3.  NOT TRUE 
4.  40 < =35 
5.  FALSE AND TRUE 
6.  60 – 15 
7.  55>10 
8.  TRUE OR FALSE 
9.  – 10> 5 
10.  10* 0.3 
Soln. :
Sr. No.  Operation  Solution 
1.  10/2  5 
2.  15 MOD 2  1 
3.  NOT TRUE  FALSE 
4.  40 < =35  FALSE 
5.  FALSE AND TRUE  FALSE 
6.  60 – 15  45 
7.  55>10  TRUE 
8.  TRUE OR FALSE  TRUE 
9.  – 10> 5  FALSE 
10.  10* 0.3  3 
Ex. 2
Using Hierarchy chart, list the order in which following operations would be processed?
1. +,,* 2. OR,*,<
3. NOT, AND,* 4. AND,OR,NOT
5. NOT,+,\ 6. MOD,\,<
7. <,AND,>,+
Soln. :
Sr. No.  Given Expression  Solution 
1  +,,*  *, +,  
2  OR,*,<  *, <, OR 
3  NOT, AND,*  *, NOT, AND 
4  AND,OR,NOT  NOT, AND, OR 
5  NOT,+,\  \, +, NOT 
6  MOD,\,<  MOD, \, < 
7  <,AND,>,+  +, <,>,AND 
Reference
 Brian W. Kernighan And Dennis M. Ritchie, The C Programming Language, Prentice Hall Of India
 YashwantKanetkar, Let Us C, Bpb Publication