Order of evaluation refers to the operator precedence and associativity rules
according to which mathematical expressions are evaluated.
Operators in the same row
have the same precedence
. The compiler will evaluate them in any order.
It may choose another order when the same expression is evaluated again, but this will not affect the evaluated answer.
Operators in different rows have different precedence. Rows are in order of decreasing precedence
. This means that below a given row, any operator in the lower rows will have lower precedence than the operators in the highest row.
The order of precedence can be altered by using parentheses
around the parts of the mathematical expression that needs to be evaluated first.
The order of evaluation in C is illustrated in the following figure:
#include <stdio.h>int main() {int answer_1 = 0; // stores order evaluated answerint answer_2 = 0; // stores left to right evaluationanswer_1 = 2 * 3 + 4 / 2;answer_2 = 2 * (3 + 4) / 2;printf("Answer with order of evaluation: %d\n", answer_1);printf("Answer without order of evaluation: %d\n", answer_2);return 0;}
Free Resources