Why do I get “a label can only be part of a statement and a declaration is not a statement” if I have a variable that is initialized after a label? [duplicate]

The language standard simply doesn’t allow for it. Labels can only be followed by statements, and declarations do not count as statements in C. The easiest way to get around this is by inserting an empty statement after your label, which relieves you from keeping track of the scope the way you would need to inside a block.

#include <stdio.h>
int main () 
    printf("Hello ");
    goto Cleanup;
Cleanup: ; //This is an empty statement.
    char *str = "World\n";
    printf("%s\n", str);

Leave a Comment