小编yok*_*232的帖子

无法在 switch case 中声明变量

#include<stdio.h>

void main()
{
    int a = 4;
    switch (a)
    {
        case 4:
            int res = 1;
            printf("%d",res);
        break;

    }
}
Run Code Online (Sandbox Code Playgroud)

当我用 gcc 编译这段代码时,我得到了错误

root@ubuntu:/home/ubuntu# gcc test.c -o t
test.c: In function ‘main’:
test.c:9:4: error: a label can only be part of a statement and a declaration is not a statement
    int res = 1;
Run Code Online (Sandbox Code Playgroud)

但是当我添加时;case 4:;我可以编译我的代码。

有什么问题,为什么要;解决这个问题?

c label declaration switch-statement

5
推荐指数
2
解决办法
93
查看次数

标签 统计

c ×1

declaration ×1

label ×1

switch-statement ×1