GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Expression Evaluation in C

Rules for expression evaluation

  • First parenthesized sub-expression is evaluated from left to right.
  • Precedence rule is applied to evaluate the sub-expression.
  • Associativity rule is applied when two or more operators is a subexpression are of the same precedence.

You may refer to the video posted at the end of this page.

Examples

Assume a = 100, b = 20, c = 10, d = 5, e = 1


Evaluate the following.

  1. a + b * c / d / e % b

Solution: We first write all the operators available along with the precedence number from the precedence chart.

If same precedence we will apply the associativity rule, for the tie breaker.

operators in the given expression are:

+ -> 4

*  -> 3

/ ->3

/ ->3

% ->3

We see that there are four operators with the same precedence. The associativity is from L->R

a + b * c / d / e % b

100 + 20 * 10 / 5 / 1 % 20

100 + 200 / 5 / 1 % 20

100 + 40 / 1 % 20

100+ 40 % 20

100 + 0

100


Evaluate ((c+d) % (d- e))

Solution: We first write all the operators available along with the precedence number from the precedence chart.

If same precedence we will apply the associativity rule, for the tie breaker.

operators in the given expression are:

 +  -> 4
%  -> 3
–  -> 4
() ->1
((c+d) % (d- e))
((10+5) % (5 – 1))
Evaluate the inner most brackets first, and the associativity is from LHS -> RHS
((10+5) % (5 – 1)) 
(15 % (5-1))
(15 % 4)
= 3

Evaluate a+2 > b && !c || a != d && a-2 <= e

a+2 > b && !c || a != d && a-2 <= e

100 + 2 >20 && !10 || 100!=5 && 100-2<=1

100 + 2 >20 && 0 || 100!=5 && 100-2<=1

102 > 20 && 0 || 100!=5 && 100-2<=1

102 > 20 && 0 || 100!=5 && 98<=1

1 && 0 || 100!=5 && 98<=1

1 && 0 || 100!=5 && 0

1 && 0 || 1 && 0

0 || 1 && 0

0 || 0

0

Evaluate 2*((8/5)+(4*(5-3)) % (8 +5 -2))

2*((8/5)+(4*(5-3)) % (8 +5 -2))

2 * (1 + (4 * (5-3)) % (8+5-2))

 2 * (1+(4 *2) % (8+5-2))

2 * (1 + 8 % (8 + 5 -2) )

2 * ( 1+8 %(13-2))

2 *(1+8%11)

2 *(1+8)

2 * 9

18

Evaluate (8-3*5) % (2+2*4) / (5- (-1))

(8-3*5) % (2+2*4) / (5- (-1))

(8-3*5) % (2+2*4) / (5-+1)

(8-15) % ( 2+2*4)/(5+1)

-7 % (2+2*4)/(5+1)

-7 %(2+8)/(5+1)

-7 % 10/(5+1)

7 % 10 / 6

7 / 6

-1

Scroll to Top