Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequence points in c

A sequence point in imperative programming 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.

What does this mean? Can somebody please explain it in simple words?

like image 330
Jagan Avatar asked Aug 26 '10 13:08

Jagan


People also ask

Is comma a sequence point in C?

The order in which the arguments to a function are evaluated are undefined as specified by the C standard. However, in the function call for printf, we have arguments that are separated by commas which classify as sequence points.

Is comma operator a sequence point?

In the C and C++ programming languages, the comma operator (represented by the token , ) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there is a sequence point between these evaluations.

What is C side effects?

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.


1 Answers

When a sequence point occurs, it basically means that you are guaranteed that all previous operations are complete.

Changing a variable twice without an intervening sequence point is one example of undefined behaviour.

For example, i = i++; is undefined because there's no sequence point between the two changes to i.

Note that it's not just changing a variable twice that can cause a problem. It's actually a change involved with any other use. The standard uses the term "value computation and side effect" when discussing how things are sequenced. For example, in the expression a = i + i++, the i (value computation) and i++ (side effect) may be done in arbitrary order.

Wikipedia has a list of the sequence points in the C and C++ standards although the definitive list should always be taken from the ISO standard. From C11 appendix C (paraphrased):


The following are the sequence points described in the standard:

  • Between the evaluations of the function designator and actual arguments in a function call and the actual call;
  • Between the evaluations of the first and second operands of the operators &&, ||, and ,;
  • Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated;
  • The end of a full declarator;
  • Between the evaluation of a full expression and the next full expression to be evaluated. The following are full expressions:
    • 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;
  • 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.
like image 172
paxdiablo Avatar answered Sep 19 '22 15:09

paxdiablo