cscopeidentifiergotoc17

strictly speaking, where does the scope of a label name starts and ends in C?


In C17's final draft N2176 document,

  1. at 1st paragraph of 6.2.1, it defines a label name as a type of identifier,
  2. at 3rd paragraph of 6.2.1, it says

A label name is the only kind of identifier that has function scope. It can be used (in a goto statement) anywhere in the function in which it appears, and is declared implicitly by its syntactic appearance (followed by a : and a statement).

  1. at 7th paragraph of 6.2.1, it says that, except a struct tag, union tag, enumeration tag, and an enumeration constant,

Any other identifier has scope that begins just after the completion of its declarator.

So, I find it conflicting to say that the scope of a label name starts at the start of a function body and end at the end of a function body, because of the paragraph 7 in 6.2.1 (to say that it starts after the completion of the label name's declaration does not makes sense when it can be accessed before its declaration).

Where exactly does the scope of a label name starts in C?


Solution

  • Section 6.2.1p3 is correct, as that explicitly talks about labels.

    If a label's scope didn't start until the point it appears, you wouldn't be able to do forward jumps like this:

    void foo()
    {
        goto bar;
        print("unreachable\n");
     bar:
        printf("at bar\n");
    }
    

    In fact, it's generally considered good practice to only use a goto to jump forward.