Expressions in C and Operator Precedence and Associativity


Expression :

Expressions in C are basically operators acting on operands. Statements like a = a + 3, ++a and
(4 * x+34) are all expressions. Strictly speaking, even a single variable or constant can be considered an expression.

Operator Precedence & Associativity :

Order of precedence means the rank in which all the operators in a c++ expression.
The order of precedence and associativity is written in the below figure.

Operator precedence Chart :

Click on the image to see it clearly...


















Examples :

This expression is evaluated in this order

1+2*2

1+(2*2)

1+2*2*4

1+((2*2)*4)

(1+2)*2*4

((1+2)*2)*4

1+4,c=2|3+5

(1+4),(c=(2|(3+5)))

1 + 5&4 == 3

(1 + 5) & (4 == 3)

c=1,99

(c=1),99

!a++ + ~f()

(!(a++)) + (~(f()))







     Prev                                                   NEXT

0 comments:

Post a Comment