2. Operators are the verbs of a language that help
the user perform computations on values.
“An operator is a symbol (+,-,*,/) that directs the computer to
perform certain mathematical or logical manipulations and is
usually used to manipulate data and variables”
Ex: a + b
Definition
2
3. Operators in C
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
3
4. Arithmetic operators
Arithmetic operators are used to perform arithmetic
operations. ‘C’ language supports following arithmetic
operators.
Operator example Meaning
+ a + b Addition –unary
- a – b Subtraction- unary
* a * b Multiplication
/ a / b Division
% a % b Modulo division-
remainder
4
5. The ‘C’ language does not provide the operator for exponentiation.
+ and – operator can be used as unary operator also. Except %
operator all arithmetic can be used with any type of numeric
operands, while % operator can only be used with integer data type
only.
Following program will clarify how arithmetic operators behave
with different data type, particularly the use of /and % operator.
Arithmetic operators
5
6. Example:-
# include<stdio.h>
main()
{
int x=25;
int y=4;
printf(“%d+%d=%dn” ,x, y, x+y);
printf(“%d-%d=%dn” ,x, y, x-y);
printf(“%d*%d=%dn” ,x, y, x*y);
printf(“%d/%d=%dn” ,x, y, x/y);
printf(“%d%%d=%dn” ,x, y, x%y);
}
Arithmetic operators
6
8. Explanation:
First three operations are obvious. The division
operation gives the answer 6 because, the variables x and
y are integer variables, when we use / with integer
operands the result will be integer number.
while,%operator produce the remainder after division of
25by 4.
Arithmetic operators
8
9. Assignment Operators
We have already used the assignment operator = in previous
programs ‘C’ language supports = assignment operator. It is used
to assign a value to a variable. The syntax is
variablename = expression
The expression can be a constant, variable name or any valid
expression.
‘C’ also supports the use of shorthand notation also.
form is
variable = varname operator expression;
into
varname operator = expression;
9
10. Assignment Operators
Use of shorthand notation makes your statement
concise and program writing becomes faster when
variable name are long in size
Assignment Operator Shorthand
a = a + 5; a += 5;
a = a – 5; a -= 5;
a = a * 5; a *= 5;
a = a /5; a /= 5;
a = a % 5(assuming a as integer) a %= 5;
10
13. Logical Operators
Sometimes in programming, we need to take certain
action if some condition are true or false. Logical
operators help us to combine more than one conditions,
and based on the outcome certain steps are taken.
Operator name Meaning
&& Logical AND
|| Logical OR
! Logical NOT(Negation)
13
14. Logical Operators
Logical NOT is an unary operator. In an expression, we can use more then
on logical operation. If more then one operator is used, !(NOT) is
evaluated first, then &&(AND) and then ||(OR), we can use parentheses to
change the order of evaluation.
for example, if we have a = 2, b = 3 and c = 5 then,
Expression Value Remark
a <b && c ==5 True Both expression are true
! (5 >3) False 5>3 is true & negation of
true is false
a< b || c=10 True a<b is true which makes
the expression true
(b> a) && (c !=5) False c =5, s0 second
condition false
(b<c || b>a) && (c==5) True Both sub expression are
true
14
15. Increment & Decrement Operators
C supports 2 useful operators namely
1. Increment (++)
2. Decrement(--)operators
The (++) operator adds a value 1 to the operand
The (--) operator subtracts 1 from the operand
(++a) or (a++)
(--a) or (a--)
15
16. Examples for (++) &(--) operators:-
Let the value of a =5 and b=++a then
a = b =6
Let the value of a = 5 and b=a++ then
a =5 but b=6
i.e.:
1. a prefix operator first adds 1 to the operand and then the result is
assigned to the variable on the left
ex:- (++a)or (--a) is called prefix increment or decrement
2. a postfix operator first assigns the value to the variable on left and
then increments the operand.
ex:- (a++)or (a--) is called postfix increment or decrement
Increment & Decrement Operators
16
17. If the ++ or – operator is used in an expression or
assignment then prefix notation give different values.
Ones should use prefix notation carefully in an
assignment or expression involving other variables.
Increment & Decrement Operators
17
18. Example:-
#include <stdio.h>
#include <conio.h>
main()
{
int x=10;
int y;
int z=0;
clrscr();
x++;
++x;
y=++x;
printf(“ Value of x=%d y=%d and z-%dn”, x,y,z);
z=y--;
printf(“ Value of x=%d y=%d and z-%dn”, x,y,z);
}
Increment & Decrement Operators
18
19. Output:-
Value of x=13 y=13 and z=0
Value of x=13 y=12 and z=13
Increment & Decrement Operators
19
20. C language has two useful operators called increment(++) and decrement
(--) that operate on integer data only.
The increment (++) operator increments the operand by 1, while the decrement
operator (--) decrements the operand by 1, for example ,:
int i , j;
i = 10;
j = i++ ;
printf(“ %d %d “, i, j);
OUTPUT:-
11 10 . First i is assigned to j and then i is incremented by 1
Increment & Decrement Operators
20
21. If we have :
int i, j ;
I = 20;
j = ++i;
printf(“%d %d”, i, j);
OUTPUT :
first i is incremented by 1 and then assignment take place i.e., pre- increment of i.
now, consider the example for (--) operator :
int a, b;
a=10;
b= a--;
printf(“%d %d”, a , b)
OUTPUT :
first a is assigned to b then a is decremented by 1. i.e.,post decrement takes place
Increment & Decrement Operators
21
22. Decrement Operator:-
If we have : int i, j ;
I = 20;
j = --i;
printf(“%d %d”, i, j);
OUTPUT : 19 19. first i is decremented by 1 and then assignment
take place i.e., pre-decrement of i.
Note : on some compilers a space is required on both sides of ++I or i++ ,
i-- or --i
Increment & Decrement Operators
22
23. Bitwise Operators
We know that internally, the data is represented in
bits 0 and 1. ‘C’ language supports some operators
which can perform at the bit level. These type of
operations are normally done in assembly or
machine level programming. But, ‘C’ language
supports bit level manipulation also, that is why ‘C’
language is also known as middle-level programming
language.
23
24. Bitwise Operators
Following table shows bit-wise operators with their
meaning:
Operator name Meaning
& Bit-wise AND
| Bit-wise OR
^ Bit-wise Exclusive OR(XOR)
<< Left Shift
>> Right Shift
- Bit-wise 1’s component
24
25. Bitwise Operators
Examples:-
#include<stdio.h>
#include<conio.h>
Void main()
{
int x;
int mul,div;
clrscr();
printf(“Give one integer numbern”);
scanf(“%d”,&x);
mul = x << 1; /* left shift */
div = x >> 1; /* right shift */
printf(“multiplication of %d by 2 = %dn”,x,mul);
scanf(“division of %d by 2 = %dn”,x,div);
}
25
27. Other Special Operators
‘C’ language provides other special operator. They
are:
Comma operator
sizeof operator
Arrow(->) operator
dot operator
* operator and & operator
27
28. Other Special Operators
Comma operator:-
(,) Comma operator is used to combine multiple statements.
It is used to separate multiple expressions. It has the lowest
precedence. It is mainly used in for loop.
The expressions which are separated by comma operator
are evaluated from left to right.
For example, the following statement
z = (x=5, x+5);
is equivalent to the statement sequence
x = 5;
z = x+5;
28
29. Other Special Operators
sizeof operator:-
sizeof operator is used to find our the storage requirement
of an operand in memory. It is an unary operator which
returns size in bytes.
The syntax is sizeof(operand)
For example,
sizeof(float) returns the value 4
sizeof(int) return the value 2
The statement sequence,
char c;
sizeof(c);
will return the value 1, because c is character type variable.
29