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. An example of unspecified behavior is Examples of situations where the order of evaluation of subexpressions or the order in which side effects take place include:
- the order in which the arguments to a function are evaluated (C99, Section 6.5.2.2, "Function calls").
- the order of evaluation of the operands in an assignment statement (C99, Section 6.5.16, "Assignment operators").
- the order in which any side effects occur among the initialization list expressions is unspecified. In particular, the evaluation order need not be the same as the order of subobject initialization (C99, Section 6.7.8, "Initialization").
Unspecified behavior is generally 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.
...
Except as specified later (for the function-call
()
,&&
,||
,?:
, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.
The order of evaluation of the operands in an assignment statement is also unspecified (C99, Section 6.5.16, "Assignment operators").
...
unspecified
...
.
...
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 or potentially confusing.
...
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 order of evaluation"
\[[MISRA 04|AA. C References#MISRA 04]\] Rule 12.2 |
...