Precedence and Associativity of Operators

Precedence: It gives the order of execution of opertaors in an expression i.e. gives an idea about which operator is to be executed first when there are multiple operators in an expression.

Example: The expression 4+3 * 2 can be solved by two ways. But only way1 is correct

Way 1) 4+3 * 2

=4+6 (3 * 2 is evaluated first)

=10

Way 2) 4+3*2

= 7*2 (4 + 3 is evaluated first)

= 14

Associativity: It tells the order whether left to right (L → R) or right to left (R → L) in which the operators of same precedence will be executed in an expression.

Example: 12 /4 *3 can be solved by two ways. But only way1 is correct because associativity is L->R for multiplication and division

Way 1) 12/4 *3

=3 * 3 (12/4 is evaluated first)

=9

Way 2) 12/4 *3

=12/12 (4 * 3 is evaluated first)

=1

Precedence and Associativity of Various Operators in C Language

error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.