C Expression Evaluation
In C programming language, expression is evaluated based on the operator precedence and associativity. When there are multiple operators in an expression, they are evaluated according to their precedence and associativity. The operator with higher precedence is evaluated first and the operator with least precedence is evaluated last.
An expression is evaluated based on the precedence and associativity of the operators in that expression.
To understand expression evaluation in c, let us consider the following simple example expression...
10 + 4 * 3 / 2
In the above expression there are three operators +, * and /. Among these three operators, both multiplication and division have same higher precedence and addition has lower precedence. So, according to the operator precedence both multiplication and division are evaluated first and then addition is evaluated. As multiplication and division have same precedence they are evaluated based on the associativity. Here, the associativity of multiplication and division is left to right. So, multiplication is performed first, then division and finally addition. So, the above expression is evaluated in the order of * / and +. It is evaluated as follows...
4 * 3 ====> 12
12 / 2 ===> 6
10 + 6 ===> 16
The expression is evaluated to 16.