编译以下代码时出现编译错误。
#include <stdio.h>
main()
{
printf("Hello 123\n");
goto lab;
printf("Bye\n");
lab: int a = 10;
printf("%d\n",a);
}
当我编译这段代码时,它给出了
test.c:8: error: a label can only be part of a statement and a declaration is not a statement
为什么标签的第一部分应该是声明而不是声明?
最佳答案
因为这个特性叫做labeled statement
C11 §6.8.1 Labeled statements
Syntax
labeled-statement: identifier : statement case constant-expression : statement default : statement
一个简单的解决方法是使用空语句(单个分号;
)
#include <stdio.h>
int main()
{
printf("Hello 123\n");
goto lab;
printf("Bye\n");
lab: ; //null statement
int a = 10;
printf("%d\n",a);
}
关于c - 为什么标签的第一部分应该是声明?为什么不声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19829531/