Variable declaration after goto Label Variable declaration after goto Label c c

Variable declaration after goto Label


The syntax simply doesn't allow it. ยง6.8.1 Labeled Statements:

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

Note that there is no clause that allows for a "labeled declaration". It's just not part of the language.

You can trivially work around this, of course, with an empty statement.

JUMP:;int a = 0;


You want a semi-colon after the label like this:

 #include <stdio.h> int main() {     int x = 5;     goto JUMP;     printf("x is : %d\n",x); JUMP: ;     /// semicolon for empty statement     int a = 0;      printf("%d",a); }    

Then your code compiles correctly for the C99 standard, with gcc -Wall -std=c99 -c krishna.c (I'm using GCC 4.6 on Debian/Sid/AMD64).


Simple explanation, other than the spec says not, is that the compiler is exepecting the code after the goto to be something that compiles into an operation which it can then calculate the offset of, and is kicking because your variable declaration isn't a statement/block that it can compile into such an offset.