Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Computer Programming

C – Operators
2021-’22 Winter B.Tech
Operators
 Arithmetic operators
 Unary operators
 Relational operators
 Logical operators
 Bitwise operators
 Assignment operators
 Conditional operators
Operator Precedence
• Precedence determines which operator is performed first in an
expression with more than one operators with different precedence.

•Operations with a higher precedence are carried out before


operations having a lower precedence

Eg: 10 + 20 * 30
is calculated as 10 + (20 * 30)

and not as (10 + 20) * 30


Associativity
• Associativity is used to determine the order in which consecutive
operations within the same precedence group are carried out.

For example: 100 / 10 * 10

- ‘*’ and ‘/’ have same precedence


- Associativity is Left to Right,

100 / 10 * 10 is treated as (100 / 10) * 10


Precedence and Associativity
Operator category Operators Associativity
- ++ -- sizeof cast !
* / %
+ -
< <= > >=
== !=
&&
||
?:
= += -= *= /= %=
Operator Precedence and Associativity

* / %
+ -
Operator Precedence and Associativity

* / %
+ -
< <= > >=
Operators
int x, a=10,b=5,c=6,d=7;
x=a>b||a<c&&c>d  x=a>b||(a<c&&c>d)
x is 1

x=(a>b||a<c)&&c>d;
< <= > >=
x is 0 &&
||
Operators
• complex logical expression will not be evaluated in its entirety if its value
can be established from its constituent operands.

error=2, count=4;
If(error > 5 && count < 100)
{…}

• Since error > 5 is false, the second operand (i.e., count < 100) will not be
evaluated, because the entire expression will be considered false.
Operators
error=2; count=4;
If(error > 5 || count < 100)
{…}
• Here, error>5 is False. Hence the Truth value of OR depends
on the second expression, and hence the second half is
evaluated.

You might also like