3

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?

Lundin
  • 195,001
  • 40
  • 254
  • 396
Kushagr Jaiswal
  • 191
  • 1
  • 9
  • 1
    "Semicolon is a sequence point" is a simplification/first approximation that doesn't capture all the nuances. The standard OTOH is the definitive source of wisdom. I'm not sure what it "kind of implies". It either implies or does not. – n. m. could be an AI Jun 01 '21 at 07:30
  • @n.'pronouns'.m. This is precisely the source for my confusion. Everywhere I look, it is directly mentioned that the semicolon is a sequence point. Even in the book from which I am studying (C Primer Plus, 6e), it is clearly mentioned that the semicolon is a sequence point. – Kushagr Jaiswal Jun 01 '21 at 07:37
  • 1
    Most semicolons are statement delimiters, so you could break the possibilities down to (1) are statements sequence points? and (2) are the other possible occurences of semicolons sequence points? Lets focus on (2) and take a semicolon within a character `';'`. Not a sequence point. End of discussion ;) – grek40 Jun 01 '21 at 07:38
  • 1
    Is there any code that would be UB depending on whether a semicolon is a sequence point? (With all the sequence points listed in the standard remaining sequence points). I don't think so! – Paul Hankin Jun 01 '21 at 07:39
  • 1
    "Semicolon is a sequence point" is an over-simplification that holds true in the majority of cases. You won't find anything in the standard explicitly stating that a semi-colon is a sequence point. However, there is a semicolon in the syntax after most of the things mentioned in that Annex C. – Lundin Jun 01 '21 at 07:44
  • @Lundin Then, shouldn't this be mentioned whenever one talks about the semicolon being a sequence point? It should be said that in the majority of cases, the semicolon is a sequence point. – Kushagr Jaiswal Jun 01 '21 at 07:47

2 Answers2

5

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;.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Although a semicolon is not a sequence point, we may ask whether the semantics specified by the C standard are the same if we add the rule “There is a sequence point at each semicolon.” The answer is no: Due to a defect in the standard, [there is no sequence point after a `return` statement](https://stackoverflow.com/questions/15637678/sequence-point-after-a-return-statement/15637783#15637783), and the behavior of `int foo(int *p) { return ++*p; } int main(void) { int a = 0; a = foo(&a); }` is not defined by the standard (due to C 2018 6.5 2) but would be if a semicolon were a sequence point. – Eric Postpischil Jun 01 '21 at 10:13
  • However, aside from that defect, would the semantics of C change if the rule were added? – Eric Postpischil Jun 01 '21 at 10:15
-3

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

Better ask yourself, why shouldn't it not be a sequence point?

break and continue are just camouflaged goto, or "jmp".

goto always needs a label, aka a pointer, aka an argument, to be able to jump, same goes for break and continue (these get their label implicit by the surrounding loop).

...

Yes, the semicolon in break; or continue; is a sequence point.

paladin
  • 765
  • 6
  • 13
  • 4
    `Yes, the semicolon in break; or continue; is a sequence point.` Why? `why shouldn't it not be a sequence point?` I asked and I do now know the answer. `goto always needs a label,` Is "needing a label" property associated with a sequence point? Why? – KamilCuk Jun 01 '21 at 08:04
  • @KamilCuk `goto x;` is a full expression, like `return x;`, so is also `break;` and `continue;` a full expression. A `;` between 2 full expressions is a sequence point. – paladin Jun 01 '21 at 08:13
  • 1
    `is a full expression` Is it a full expression? I do not see it listed. `return` is listed, `goto` is not. – KamilCuk Jun 01 '21 at 08:14
  • This list is incomplete, it's just an examples list. Only relevant is -> "Between the evaluation of a full expression and the next full expression to be evaluated." -> so look for the definition of a "full expression". – paladin Jun 01 '21 at 08:15
  • As a rule of thumb, when answering "language-lawyer" type questions, **never** ask yourself, and **always** ask the standard specification or similar source. – grek40 Jun 01 '21 at 08:18
  • @grek40 Sorry, but `goto x;` is clearly a full expression. Try to disprove me. – paladin Jun 01 '21 at 08:24
  • @paladin in fact, my comment doesn't depend on whether you are right or wrong. It targets your structure of reasoning rather than the resulting statement. You can't deny your answer starts with "Better ask yourself". I comment that this approach is not viable for this kind of question. – grek40 Jun 01 '21 at 08:29
  • A label used by goto falls under the synax category called labeled-statement, which has to be used together with another statement to make sense, the syntax being `identifier : statement`. Where "statement" can for example be a null statement expression `;`. This label syntax has nothing to do with break or continue since they don't come with label identifiers in their syntax. – Lundin Jun 01 '21 at 09:19
  • 1
    @paladin: `goto x;` is not a full expression as defined by C 2018 6.8 4 because it is not an expression at all. Expressions are defined by C 2018 6.5, and the grammar for them does not include `goto` statements. – Eric Postpischil Jun 01 '21 at 10:00
  • @EricPostpischil _"A full expression is an **expression** that is not part of another expression or of a declarator."_ -> _"An expression is a sequence of **operators** and **operands** that specifies computation of a value, or **that designates an object** or a function, or that generates side effects, or that performs a combination thereof."_ -> a pointer to a variable (operand) is an operator -> `void* label_pointer; MYLABEL: label_pointer = &MYLABEL; goto *label_pointer;` -> `goto x;` is a full expression – paladin Jun 01 '21 at 10:31
  • 4
    @paladin: One, labels as values are a GCC extension and so are not relevant to what the C standard specifies. Two, `goto *label_pointer;` is not `goto x;`. Even if GCC defines its extension to include an expression in its extended `goto` statement, that does not mean there is an expression in a standard `goto` statement. For that matter, even if GCC defined the standard `goto` statement to contain or to be an expression, that would not change what the C standard says. The C standard defines expressions with a formal grammar in 6.5, and `goto` is not in it. – Eric Postpischil Jun 01 '21 at 10:42
  • _`goto x;` is a full expression, like `return x;`_ But `return x;` is not a full expression. `x` in `return x;` is. – Language Lawyer Jun 01 '21 at 15:45