Search code examples
clanguage-lawyerbreakcontinuesequence-points

Is the semicolon really a sequence point in C?


According to this answer, the following are the sequence points described in the standard:

  1. Between the evaluations of the function designator and actual arguments in a function call and the actual call;

  2. Between the evaluations of the first and second operands of the operators &&, ||, and ,;

  3. Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated;

  4. The end of a full declarator;

  5. 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.
  6. Immediately before a library function returns;

  7. After the actions associated with each formatted input/output function conversion specifier;

  8. 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 standard never explicitly mentions that the semicolon is a sequence point, but the various sequence points that have been stated kind of imply that the semicolon is indeed a sequence point.

So, is the semicolon in break; or continue; a sequence point?


Solution

  • Is the semicolon really a sequence point in C?

    No. Specific semantic language constructs are specifically required to have a sequence point after evaluating them. (like, ex. Logical AND operator ...if the second operand is evaluated, there is a sequence point between... - it's specific). A sequence point is indeed related to, like, semantics ("evaluation of this happens before that") rather than to tokens ("everything happens before the ; character").

    So, is the semicolon in break; or continue; a sequence point?

    No, it is not. Together with goto they look like a exception to the colloquial rule.

    It is not a function call, not a logical operator && ||, not , operator, not a ternary ?: operator, not a declaration, not a full expression - it's not listed in the list you quoted (the list is from ANNEX C), it's not anyhow volatile and does no I/O. So, well, under the "when you have eliminated the impossible, whatever remains, however improbable, must be the truth" logic there is indeed no sequence point after break; nor continue;.