Variable declaration after goto Label

CGotoVariable Declaration

C Problem Overview


Today I found one interesting thing. I didn't know that one can't declare a variable after a goto label.

Compiling the following code

#include <stdio.h>
int main() {
    int x = 5;
    goto JUMP;
    printf("x is : %d\n",x);
JUMP:
    int a = 0;  <=== giving me all sorts of error..
    printf("%d",a);
}

gives errors like

temp.c: In function ‘main’:
temp.c:7: error: expected expression before ‘int’
temp.c:8: error: ‘a’ undeclared (first use in this function)
temp.c:8: error: (Each undeclared identifier is reported only once
temp.c:8: error: for each function it appears in.)

Now what is the logic behind that? I heard that one cannot create variables inside the case statements of switch. Since JUMP is inside the same scope (the scope of main function, in my case) of the goto statement, I believe that scope is not an issue here. But then, why am I getting this error?

C Solutions


Solution 1 - C

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;

Solution 2 - C

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).

Solution 3 - C

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.

Solution 4 - C

My gcc version (4.4) is giving this compile error:

t.c:7: error: a label can only be part of a statement and a declaration is not a statement

. This error-message says it all.

Solution 5 - C

Well, first you should be consistent. It's either LABEL or label. Second, label is a part of the statement and the declaration doesn't answer the description enough.

You can replace LABEL: with label: ; and then it is likelier to compile.

EDIT: Now that you edited your code all over, it should be JUMP: replaced with JUMP: ; ;-)

Solution 6 - C

If you know why you can't create variables inside case statement of switch, basically its the same reason why you cant do this too. As a fix, you can try this,

#include <stdio.h>
int main() {
    int x = 5;
    goto JUMP;
    printf("x is : %d\n",x);
JUMP:
    {                                              //Note this
       int a = 0;  // <=== no more error..
       printf("%d",a);
    }                                             //Note this
}

Solution 7 - C

#include <stdio.h>
int main() {
    int x = 5;
    goto JUMP;
    printf("x is : %d\n",x);
JUMP:
    printf("Do anything after label but dont declare 
	anything. even empty statement will also work 
	because label can only be part of a statement");
    int a = 0;  
    printf("%d",a);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKrishnabhadraView Question on Stackoverflow
Solution 1 - CStephen CanonView Answer on Stackoverflow
Solution 2 - CBasile StarynkevitchView Answer on Stackoverflow
Solution 3 - CjmorenoView Answer on Stackoverflow
Solution 4 - CPatrick B.View Answer on Stackoverflow
Solution 5 - CMichael Krelin - hackerView Answer on Stackoverflow
Solution 6 - CCOD3BOYView Answer on Stackoverflow
Solution 7 - CJeegar PatelView Answer on Stackoverflow