The order in which operands in an expression are evaluated is undefined in C except at the sequence points.
Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.
The following are the sequence points described:
- The call to a function, after the arguments have been evaluated.
- The end of the first operand of the following operators: logical AND &&; logical OR ||; conditional ?; comma ,.
- The end of a full declarator: declarators;
- The end of a full expression: an initializer; the expression in an expression statement; the controlling expression of a selection statement (if or switch); the controlling expression of a while or do statement; each of the expressions of a for statement; the expression in a return statement.
- Immediately before a library function returns (7.1.4).
- After the actions associated with each formatted input/output function conversion specifier.
- Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call.
Non-compliant Code Example
Compliant Solution
References
C99 5.1.2.3 Program execution
C99 Annex C Sequence points