Variable declaration after goto Label

こ雲淡風輕ζ 提交于 2019-12-28 04:54:12

问题


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?


回答1:


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;



回答2:


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




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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
}



回答7:


It's not because of the label per se, it's because there are already statements (goto and printf). The latest standard seems to allow variable declarations in arbitrary places, but not every compiler fully conforms to the standard. Also, identifiers are case-sensitive in C and your label must be the same in both places.




回答8:


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


来源:https://stackoverflow.com/questions/8384388/variable-declaration-after-goto-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!