Operator performs an operation on data.
They are classified into following −
Arithmetic operators.
Relational operators.
Logical operators.
Assignment operators.
Increment and decrement operators.
Bitwise operators.
Conditional operators.
Special operators.
Arithmetic operator
These operators are used for numerical calculations (or) to perform arithmetic operations like
addition, subtraction etc.
Operator Description Example a=20,b=10 Output
+ Addition a+b 20+10 30
- subtraction a-b 20-10 10
* multiplication a*b 20*10 200
/ Division a/b 20/10 2(quotient)
% Modular Division a%b 20%10 0 (remainder)
Program
Following is the C program for arithmetic operator −
#include<stdio.h>
main ( ){
int a= 20, b = 10;
printf (" %d", a+b);
printf (" %d", a-b);
printf (" %d", a*b);
printf (" %d", a/b);
printf (" %d", a%b);
}
Output
When the above program is executed, it produces the following result −
30
10
200
20
Relational operators
These are used for comparing two expressions.
Operator Description Example a=20,b=10 Output
< less than a<b 10<20 1
<= less than (or) equal to a<=b 10<=20 1
> greater than a>b 10>20 0
>= greater than (or) equal to a>=b 10>=20 0
== equal to a==b 10==20 0
!= not equal to a!=b 10!=20 1
The output of a relational expression is either true (1) (or) false (0).
Program
Following is the C program for relational operator −
#include<stdio.h>
main ( ){
int a= 10, b = 20;
printf (" %d", a<b);
printf (" %d", a<=b);
printf (" %d", a>b);
printf (" %d", a>=b);
printf (" %d", a = =b);
printf (" %d", a ! =b);
}
Output
When the above program is executed, it produces the following result −
110001
Logical Operators
These are used to combine 2 (or) more expressions logically.
They are logical AND (&&) logical OR ( || ) and logical NOT (!)
exp1 exp2 exp1&&exp2
T T T
T F F
F T F
F F F
Logical AND(&&)
exp1 exp2 exp1||exp2
T T T
T F T
F T T
F F F
Logical OR(||)
exp !exp
T F
F T
Logical NOT(!)
Operator Description Example a=20,b=10 Output
&& logical AND (a>b)&&(a<c) (10>20)&&(10<30) 0
|| logical OR (a>b)||(a<=c) (10>20)||(10<30) 1
! logical NOT !(a>b) !(10>20) 1
Program
Following is the C program for logical operator −
#include<stdio.h>
main ( ){
int a= 10, b = 20, c= 30;
printf (" %d", (a>b) && (a<c));
printf (" %d", (a>b) | | (a<c));
printf (" %d", ! (a>b));
}
Output
When the above program is executed, it produces the following result −
011
Assignment operators
It assigns a value to a variable. The types of assignment operators are −
Simple assignment.
Compound assignment.
Operator Description Example
= simple assignment a=10
+=,-=,*=,/=,%= compound assignment a+=10"a=a+10a-=10"a=a-10
Program
Following is the C program for assignment operator −
#include<stdio.h>
main ( ){
int a= 10,;
printf (" %d", a);
printf (" %d", a+ =10);
}
Output
When the above program is executed, it produces the following result −
10
20
Increment and decrement operator
Let us understand what is an increment operator.
Increment operator (++)
This operator increments the value of a variable by 1
The two types include −
pre increment
post increment
If we place the increment operator before the operand, then it is pre-increment. Later on, the
value is first incremented and next operation is performed on it.
For example,
z = ++a; // a= a+1
z=a
If we place the increment operator after the operand, then it is post increment and the value
is incremented after the operation is performed.
For example,
z = a++; // z=a
a= a+1
Example
Following is the C program for increment operator −
Program Program
main() { main() {
int a= 10, z; int a= 10, z;
z= ++a; z= a++;printf("z=%d", z);
printf("z=%d", z); printf("a=%d", a);
printf("a=%d", a); }
}
Output Output
z= 11 z= 10
a=11 a=11
Decrement operator − (- -)
It is used to decrement the values of a variable by 1.
The two types are −
pre decrement
post decrement
If the decrement operator is placed before the operand, then it is called pre decrement. Here,
the value is first decremented and then, operation is performed on it.
For example,
z = - - a; // a= a-1
z=a
If the decrement operator is placed after the operand, then it is called post decrement. Here,
the value is decremented after the operation is performed.
For example,
z = a--; // z=a
a= a-1
main() { main() {
int a= 10, z; int a= 10, z;
z= --a; z= a--;
printf("z=%d", z); printf("z=%d", z);
printf("a=%d", a); printf("a=%d", a);
} }
Output Output
z= 9 z= 10
a=9 a=9
Bitwise Operator
Bitwise operators operate on bits.
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Left Shift
>> Right shift
~ One's Complement
Bitwise AND
a b a&b
0 0 0
0 1 0
1 0 0
1 1 1
Bitwise OR
a b a|b
0 0 0
0 1 1
1 0 1
1 1 1
Bitwise XOR
a b a^b
0 0 0
0 1 1
1 0 1
1 1 0
Program
Following is the C program for bitwise operator −
#include<stdio.h>
main ( ){
int a= 12, b = 10;
printf (" %d", a&b);
printf (" %d", a| b);
printf (" %d", a ^ b);
}
Output
When the above program is executed, it produces the following result −
8 14 6
Left Shift
If the value is left shifted one time, then its value gets doubled.
For example, a = 10, then a<<1 = 20
Right shift
If the value of a variable is right shifted one time, then its value becomes half the original
value.
For example, a = 10, then a>>1 = 5
Ones complement
It converts all ones to zeros and zeros to ones.
For example, a = 5, then ~a=2 [only if 4 bits are considered].
Program
Following is another C program for bitwise operator −
#include<stdio.h>
main ( ){
int a= 20, b = 10,c=10;
printf (" %d", a<<1);
printf (" %d", b>>1);
printf (" %d", ~c);
}
Output
When the above program is executed, it produces the following result −
40
5
11
Signed
1’s complement = - [ give no +1]
For example, ~10 = - [10+1] = -11
~-10 = - [-10+1] = 9
Unsigned
1’s complement = [65535 – given no]
Conditional operator (? :)
It is also called ternary operator.
The syntax is as follows −
exp1? exp2: exp3
If exp1 is true, exp2 is evaluated. Otherwise, exp3 is evaluated. Or in the form of if-else.
if (exp1)
exp2;
else
exp3;
Program
Following is the C program for conditional operator −
#include<stdio.h>
main ( ){
int z;
z = (5>3) ? 1:0;
printf ("%d",z);
}
Output
When the above program is executed, it produces the following result −
Special operations
Some of the special operations are comma, ampersand (&), size of operators.
Comma ( , ) − It is used as separator for variables. For example; a=10, b=20
Address (&) − It get the address of a variables.
Size of ( ) − It used to get the size of a data type of a variable in bytes.
dot (.) and arrow (->) Operators
Member operators are used to referencing individual members
of classes, structures, and unions.
The dot operator is applied to the actual object.
The arrow operator is used with a pointer to an object.
Cast Operator
Casting operators convert one data type to another. For example,
int(2.2000) would return 2.
A cast is a special operator that forces one data type to be converted
into another.
The most general cast supported by most of the C compilers is as
follows − [ (type) expression ].
Program
Following is the C program for special operations −
#include<stdio.h>
main ( ){
int a=10;
float b=20 ;
printf (" a= %d b=%f", a,b );
printf (" a address =%u
" , &a ) ;
printf (" b address =%u
" ,&b ) ;
printf ("a size = %ld
" , sizeof (a) ) ;
printf ( "b size = %ld ", sizeof (b) ) ;
}
Output
When the above program is executed, it produces the following result −
a=10 b=20.00
Address of a =1 2 3 4
Address of b = 5 6 7 8 Only for this example
Size of a = 4 bytes
Size of b = 4 bytes
C Operator Precedence
At first, the expressions within parenthesis are evaluated. If no parenthesis
is present, then the arithmetic expression is evaluated from left to right.
There are two priority levels of operators in C.
High priority: * / %
Low priority: + –
The evaluation procedure of an arithmetic expression includes two left to
right passes through the entire expression. In the first pass, the high priority
operators are applied as they are encountered and in the second pass, low
priority operations are applied as they are encountered.
Suppose, we have an arithmetic expression as:
x = 9 – 12 / 3 + 3 *2 - 1
This expression is evaluated in two left to right passes as:
First Pass
Step 1: x = 9-4 + 3 * 2 – 1
Step 2: x = 9 – 4 + 6 – 1
Second Pass
Step 1: x = 5 + 6 – 1
Step 2: x = 11 – 1
Step 3: x = 10
But when parenthesis is used in the same expression, the order of
evaluation gets changed.
For example,
x = 9 – 12 / (3 + 3) * (2 – 1)
When parentheses are present then the expression inside the parenthesis
are evaluated first from left to right. The expression is now evaluated in
three passes as:
First Pass
Step 1: x = 9 – 12 / 6 * (2 – 1)
Step 2: x= 9 – 12 / 6 * 1
Second Pass
Step 1: x= 9 – 2 * 1
Step 2: x = 9 – 2
Third Pass
Step 3: x= 7
There may even arise a case where nested parentheses are present (i.e.
parenthesis inside parenthesis). In such case, the expression inside the
innermost set of parentheses is evaluated first and then the outer
parentheses are evaluated.
For example, we have an expression as:
x = 9 – ((12 / 3) + 3 * 2) – 1
The expression is now evaluated as:
First Pass:
Step 1: x = 9 – (4 + 3 * 2) – 1
Step 2: x= 9 – (4 + 6) – 1
Step 3: x= 9 – 10 -1
Second Pass
Step 1: x= - 1 – 1
Step 2: x = -2
Note: The number of evaluation steps is equal to the number of
operators in the arithmetic expression.