您的位置:首页 > 其它

switch-case 中的分支中变量定义 编译不通过问题

2010-03-31 09:42 357 查看
如下的函数代码,程序无法编译通过。

#include <stdio.h>
void test_switch(int val)
{
switch (val)
{
case 1:
int abc;
printf("you input 1/n");
break;
case 2:
printf("you input 2/n");
break;
case 3:
printf("you input 3/n");
break;
default:
printf("Not deal with: %d/n", val);
break;
}
return;
}
int main()
{
int i;
scanf("%d", &i);
test_switch(i);
return 0;
}


 

报错如下:

sea@sea-server:~/testcode$ gcc switch.c

switch.c: In function 'test_switch':

switch.c:8: error: expected expression before 'int'

 

解决的方法有两个:

a. 使用分括号加case的分支整体包含进来,如下:

    case 1:

    {

         int abc;

         ...

    }

    break;

 b. 在定义变量前,加个printf也行,如下:

    case 1:

          printf("");

          int abc;

          ...

     break;

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  input function gcc