The order of evaluation of subexpressions , and the order in which side effects take place , are frequently defined as unspecified behavior by C99. Counter intuitively, unspecified behavior is where the standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance. Consequently, unspecified behavior can be a portability issue, as different implementations can make different choices. If dynamic scheduling is used, however, there may not be a fixed-code execution sequence over the life of a process. Operations that can be executed in different orderings may in fact be executed in a different order.
...
This recommendation is related to EXP30-C. Do not depend on order of evaluation between sequence points, but focuses on behavior that is non-portable nonportable or potentially confusing.
...
This compliant solution is independent of the order of evaluation of the operands and can only be interpreted in only one way.
Code Block | ||
---|---|---|
| ||
#include <stdio.h> int g; int f(int i) { g = i; return i; } int main(void) { int x = f(1); x += f(2); printf("g = %d\n", g); /* ... */ return 0; } |
...
EXP10-EX4: The left operand of a comma operator is evaluated followed by before the right operand is evaluated. There is a sequence point in between.
Note that while commas serve to delimit multiple arguments in a function call, these commas are not considered "comma operators." Multiple arguments of a function call may be evaluated in any order, with no sequence points in between each other.
Risk Assessment
...
Wiki Markup |
---|
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.5, "Expressions," Section 6.5.16, "Assignment operators," Section 6.5.2.2, "Function calls," and Section 6.7.8, "Initialization" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "JCW Operator precedence/Order of Evaluation" and "SAM Side-effects and orderOrder of evaluationEvaluation" \[[MISRA 04|AA. C References#MISRA 04]\] Rule 12.2 |
...