Lecture CSP03
Lecture CSP03
Arithmetic Operators
Relational Operators
Assignment Operators
Logical Operators
Bitwise Operators
Increment and Decrement Operators
Conditional Operators
comma operator
Special Operators
1
Operators
• Unary
• Binary
• Ternary
Arithmetic Operators
Operator Meaning
* Multiplication
/ Division
% Modulo Division
(not applied for float and double)
3
Arithmetic Expression
4
Example1.c
main()
{ int x, y, z;
x=1;
y=3; z=10;
printf(“ print values of x=%d, y=%d, z=%d“, x, y, z);
x=x+y;
printf(“print value of x=%d”,x);
x=x-y;
printf(“value of x=%d”,x);
x=4; //RESET
z=z*x;
printf(“value of z=%d”,z);
z=15; //Reset
z=z/y;
printf(“value of z=%d”,z);
}
Examples:
Write a equivalent C expression for given algebraic expressions.
1. (a+b)(c+d)
2. ax2+bx+c
6
COMBAINING ARITHMETIC OPERATOR WITH =
Z= X+Y;
X=X+Y;
The combination of assignment operator (=) with the arithmetic
operators (+,-,*,/, %) gives the arithmetic assignment operator.
Z = Z*X+Y;
Z*= X+Y; ( T / F)
X=X+Y; ????
X=1;
X+=Y; ???
X=1;
Z=Z*X+Y; ???
Z=10;
Z=Z*(X+Y); ???
Z=10;
Z*=X+Y ????
EXAMPLE
X=X+Y; ???? 4
X=1; /* RESET */
X+=Y; ??? 4
X=1; /* RESET */
Z=Z*X+Y; ??? 13
Z=10; /* RESET */
Z=Z*(X+Y); ??? 40
Z=10; /* RESET */
Z*=X+Y ???? 40
Unary minus
13
Operators
Associatively
() [] ->
*/%+-
<< >>
< <= > >=
== !=
• Left Right
&
^
|
&& ||
,
?:
= += -= *= /= %= &= • Right Left
^= |= <<= >>=
Relational Operators
Operator Meaning
< 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
15
Examples:
5 <= 7 TRUE
5 < -8 FALSE
11 < 8+4 TRUE
x+y == a+b TRUE if value of x+y is equal to value of a+b
-20 >= 0 FALSE
1. (a+b)(c+d)
2. ax2+bx+c
3. pr2+2prh
4. s = ut + 1/2at2
5. T = (m1m2/m1+m2)
17