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

前端 未结 3 1165

I am writing some C code and in my code I have two nested loops. On a particular condition I want to break out of the inner loop and continue the o

3条回答
  •  情歌与酒
    2020-12-11 00:18

    The label should point to a statement.

    C mandates this:

    (C99, 6.8.1 Labeled statements p4) " Any statement may be preceded by a prefix that declares an identifier as a label name."

    In your case you can use a null statement:

    void foo(void)
    {
        goto bla;
    
        bla:
        ;
     }
    

    Null statements perform no operation.

    Or you can also use a compound statement (a block) if you have declarations:

    void foo(void)
    {
        goto bla;
    
        bla:
        {
            int x = 42;
            printf("%d\n", x);
        }
     }
    

提交回复
热议问题