Presentation of C Programming Operators and Expressions
Presentation of C Programming Operators and Expressions
and
Expressions (1)
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.
7
#include<stdio.h>
int main()
{
int a,b;
...
...
return 0;
}
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
12
Relational Operators
❑ In C we are having relational operator
for comparing different relations.
i.e. 4<5, 7>3.
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
15
It means first both side’s arithmetic expressions
are evaluated and then the results are compared.
12 < 18 (True → 1)
16
Logical Operators
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…
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