Krishnabhadra
Krishnabhadra

Reputation: 34275

Variable declaration after goto Label

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?

Upvotes: 81

Views: 33864

Answers (7)

jmoreno
jmoreno

Reputation: 13561

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.

Upvotes: 7

Jeegar Patel
Jeegar Patel

Reputation: 27210

#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);
}

Upvotes: 0

COD3BOY
COD3BOY

Reputation: 12092

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
}

Upvotes: 6

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

Upvotes: 18

Stephen Canon
Stephen Canon

Reputation: 106167

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;

Upvotes: 106

Patrick B.
Patrick B.

Reputation: 12353

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.

Upvotes: 6

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143081

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: ; ;-)

Upvotes: 2

Related Questions