Versions Compared

Key

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

...

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.

The following are the sequence points defined by C99:

  • The the call to a function, after the arguments have been evaluated.
  • The the end of the first operand of the following operators: logical AND &&; logical OR ||; conditional ?; comma ,.
  • The the end of a full declarator: declarators;
  • The 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 immediately before a library function returns (7.1.4).
  • After after the actions associated with each formatted input/output function conversion specifier.
  • Immediately 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.

Between the previous and next sequence point an object can only have its stored value modified once by the evaluation of an expression. Additionally, the prior value can be read only to determine the value to be stored.

This rule means that statements such as:

Code Block
i = i + 1;

are allowed, while statements like:

Code Block
i = i++;

are not allowed because it modifies they modify the same value twice.

Non-Compliant Code Example

...

If i was equal to 0 before the statement, this statement may be result in the following outcome:

Code Block
a = 0 + b[1];

Or it may also legally result in the following outcome:

Code Block
a = 1 + b[1];

As a result, programs can not cannot safely rely on the order of evaluatoin of operands between sequence pionts.

...

There is no ordering of subexpressions implied by the assignment operator, so the behavior of the following these statements is undefined:.

Code Block
i = ++i + 1;
a[i++] = i;

...

These statements are allowed by the standard:.

Code Block
i = i + 1;
a[i] = i;

...

Compliant Solution

This solution is appropiate appropriate when the programmer intends for both arguments to func() to be equivalent:.

Code Block
i++;
func(i, i);

This solution is appropiate appropriate when the programmer intends for the second argument to be one greater than the first:.

Code Block
j = i;
j++;
func(i, j);

...