Operators
and
Expressions (1)
Prepared by Dr. Mehdi Hasan Chowdhury, EEE, CUET
❑ An operator is a symbol that tell the computer
to perform certain mathematical or logical
manipulation.
❑ C supports a rich set of operators
2
❑ C language uses many types of operators as
listed below:-
1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Increment and Decrement Operators
5) Assignment Operators
6) Conditional Operators
7) Bitwise Operators
8) Special Operators
3
Arithmetic Operators
❑C provides all the basic arithmetic operators.
❑The operator +, -, * and / all work same way as in
other languages.
❑These can operate on any built-in data type
allowed in C.
4
Arithmetic Operators
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division (reminder)
5
Integer Arithmetic
❑ When both the operands in a single arithmetic
expression such as a+b are integers, the
expression is called the integer expression, and
the operation is called as integer arithmetic.
❑ Integer arithmetic always results in an integer
value.
❑ The largest integer value depends on the
machine.
6
Integer Arithmetic
Here, a and b are integers,
if a= 14 and b=4 then,
a – b = 10
a + b = 18
a * b = 56
a / b = 3 (decimal part truncated)
a % b = 2 (reminder of division)
7
#include<stdio.h>
int main()
{
int a,b;
...
...
return 0;
}
a = 16, b = 5 a/b = 3 a%b = 1
a = -16, b = 5 a/b = -3 a%b = -1
a = 16, b = -5 a/b = -3 a%b = 1
a = -16, b = -5 a/b = 3 a%b = -1
8
9
Real Arithmetic
❑ An arithmetic involving only real operands is
called real arithmetic.
❑ A real operand may can have values either in
decimal notation or in exponent notation.
❑ Since floating point values are rounded to the
number of significant digits permissible, the
final value is an approximation of the correct
result.
10
i.e, 6.0/7.0 =0.857143
-2.0/3.0 = -0.666667
If a is integer type ,then
a=6.0/7.0;
printf(“value of a is:%d”,a);
o/p: Value of a is: 0
✓ The operator modulo(%) cannot be
used with real operands. 11
Mixed-mode Arithmetic
❑ When one operand is real and the other is
integer, the expression is called a mixed-mode
arithmetic expression.
❑ If either operand is of real type, then only the
real operation is performed, and the result is
always a real number. Example:
15 / 10.0 = 1.5
where as, 15 / 10 = 1
12
Relational Operators
❑ In C we are having relational operator
for comparing different relations.
i.e. 4<5, 7>3.
❑ Any relational expression can have
either true or false value.
i.e. 4<5 is true →1
4>5 is false → 0
13
Different Relational Operators
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
14
General Form:
a_exp1 relational_operator a_exp2
Like, 5+7 < 9+9
✓ When arithmetic expressions are used on
either side of a relational operator, the
arithmetic operators have a higher priority over
relational operators.
15
It means first both side’s arithmetic expressions
are evaluated and then the results are compared.
So, 5+7 < 9+9
12 < 18 (True → 1)
16
Logical Operators
Logical Operators are used when we want to
test more than one condition and make
decisions. Example:
if (age>50 && salary<1000)
if (number<0 || number>100)
17
Truth Table
OP1 && OP2 OP1 || OP2
OP-1 OP-2
(And) (Or)
Non-Zero Non-Zero 1 1
Non-Zero 0 0 1
0 Non-Zero 0 1
0 0 0 0
18
Assignment Operators
❑ Assignment operators are used to assign
the result of an expression to a variable.
❑ Assignment operator is ‘=‘
❑ What is the difference between ‘=‘ and ‘==‘
operators?
i = 5;
if (i == 5)
19
Shorthand Assignment Operators
X += Y;
is same as
X = X+Y;
Means,
Variable OP= exp;
is same as
Variable = Variable OP (exp);
20
Shorthand Assignment Operators…
Statement with simple Statement with shorthand
assignment operator assignment operator
a= a+1 a+= 1
a= a-1 a-= 1
a= a*(n+1) a*= n+1
a= a/(n+1) a/= n+1
a= a % b a%= b
21
Three Advantages of
Shorthand Assignment Operators
22
23
24