In C why do you need a statement after a goto label?

In the standard it’s explicitly said that labels belong to a statement, therefore a simple semicolon (;) after your label can circumvent the problem you are running in to, since that counts as a statement.

There is even an example of the use of an “empty1 statement in 6.8.3/6.

EXAMPLE 3 A null statement may also be used to carry a label just
before the closing } of a compound statement

while (loop1) {
  /* ... */

  while (loop2) {
    /* ... */

    if (want_out)
      goto end_loop1;

    /* ... */
  }

  /* ... */

  end_loop1: ;
}

1 In the standard this is referred to as a null statement.


6.8.1 Labeled statements

Syntax
  1 labeled-statement:
      identifier : statement
      case constant-expression : statement
      default : statement

Notice that statement isn’t optional in the above quotation.


  • open-std.org: n1124.pdf

Leave a Comment