Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The order in which operands in an expression are evaluated is unspecified in C. The only guarantee is that they will all be completely evaluated at the next sequence point.

Evaluation of an expression may produce side effects. At specific points in the execution sequence called sequence points, all side effects of previous evaluations have completed, and no side effects of subsequent evaluations have yet taken place.

Wiki Markup
The following
are the
 sequence points are defined by C99 \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\]:

  • 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, but see the note below).
  • The end of a full declarator.
  • 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.

...

The call to func() has undefined behavior because there 's are no sequence point points between the argument expressions. The first (left) argument modifies i. It also expression reads the value of i, but only (to determine the new value to be stored in ) and then modifies i. So far, so good. However, the The second (right) argument expression reads the value of i between the same pair of sequence points as the first argument, but not to determine the value to be stored in i. This additional attempt to read the value of i has undefined behavior.

...

Wiki Markup
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 5.1.2.3, "Program execution,"
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.5, "Expressions"
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\]," Annex C, "Sequence points"
\[[Summit 05|AA. C References#Summit 05]\] Questions 3.1, 3.2, 3.3, 3.3b, 3.7, 3.8, 3.9, 3.10a, 3.10b, 3.11
\[[Saks 07|AA. C References#Saks 07]\]