C Programming/Side effects and sequence points

In C and more generally in computer science, a function or expression is said to have a side effect if it modifies a state outside its scope or has an observable interaction with its calling functions or the outside world. By convention, returning a value has an effect on the calling function, but this is usually not considered as a side effect.

Some side effects are:
 * Modification of a global variable or static variable
 * Modification of function arguments
 * Writing data to a display or file
 * Reading data
 * Calling other side-effecting functions

In the presence of side effects, a program's behaviour may depend on history; that is, the order of evaluation matters. Understanding and debugging a function with side effects requires knowledge about the context and its possible histories.

A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C, because they are a core concept for determining the validity and, if valid, the possible results of expressions. Adding more sequence points is sometimes necessary to make an expression defined and to ensure a single valid order of evaluation.


 * 1) An expression's evaluation can be sequenced before that of another expression, or equivalently the other expression's evaluation is sequenced after that of the first.
 * 2) The expressions' evaluation is indeterminately sequenced, meaning one is sequenced before the other, but which is unspecified.
 * 3) The expressions' evaluation is unsequenced.

The execution of unsequenced evaluations can overlap, with catastrophic undefined behavior if they share state. This situation can arise in parallel computations, causing race conditions.

Examples of ambiguity
Consider two functions  and. In C, the  operator is not associated with a sequence point, and therefore in the expression   it is possible that either   or   will be executed first. The comma operator introduces a sequence point, and therefore in the code  the order of evaluation is defined: first   is called, and then   is called.

Sequence points also come into play when the same variable is modified more than once within a single expression. An often-cited example is the C expression, which apparently both assigns   its previous value and increments. The final value of  is ambiguous, because, depending on the order of expression evaluation, the increment may occur before, after, or interleaved with the assignment. The definition of a particular language might specify one of the possible behaviors or simply say the behavior is undefined. In C, evaluating such an expression yields undefined behavior.

In C, sequence points occur in the following places.


 * 1) Between evaluation of the left and right operands of the && (logical AND), || (logical OR) (as part of short-circuit evaluation), and comma operators. For example, in the expression, all side effects of the sub-expression   are completed before any attempt to access.
 * 2) Between the evaluation of the first operand of the ternary "question-mark" operator and the second or third operand. For example, in the expression  there is a sequence point after the first , meaning it has already been incremented by the time the second instance is executed.
 * 3) At the end of a full expression. This category includes expression statements (such as the assignment ), return statements, the controlling expressions of ,  ,  , or  -  statements, and all three expressions in a   statement.
 * 4) Before a function is entered in a function call. The order in which the arguments are evaluated is not specified, but this sequence point means that all of their side effects are complete before the function is entered. In the expression,   is called with a parameter of the original value of  , but   is incremented before entering the body of  . Similarly,   and   are updated before entering   and   respectively. However, it is not specified in which order  ,  ,   are executed, nor in which order  ,  ,   are incremented. If the body of   accesses the variables   and  , it might find both, neither, or just one of them to have been incremented. (The function call   is not a use of the comma operator; the order of evaluation for  ,  , and   is unspecified.)
 * 5) At a function return, after the return value is copied into the calling context. (This sequence point is only specified in the C++ standard; it is present only implicitly in C.)
 * 6) At the end of an initializer; for example, after the evaluation of  in the declaration.
 * 7) Between each declarator in each declarator sequence; for example, between the two evaluations of  in  . (This is not an example of the comma operator.)
 * 8) After each conversion associated with an input/output format specifier. For example, in the expression, there is a sequence point after the   is evaluated and before printing.