今天我发现了一件有趣的事情。我不知道不能在 goto 标签之后声明变量。
编译以下代码
#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);
}
给出错误,如
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.)
现在这背后的逻辑是什么?我听说不能在 switch 的 case 语句中创建变量。由于 JUMP 在 goto 语句的同一范围内(在我的例子中是 main 函数的范围),我相信范围在这里不是问题。但是,为什么我会收到这个错误?