为什么C标准允许这个声明 [英] why does C standard allow this declaration

查看:107
本文介绍了为什么C标准允许这个声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




为什么C允许在开关块内声明变量。

ex:foll prg不给出未声明的b"错误消息但也确实

没有将b初始化为20

int a = 1;

开关(a)

{

int b = 20;

案例1:printf(b是%d \ n,b);

break;

默认:printf(" b是%d \ nn,b);

休息;

}


打印出一些垃圾。

(是的,即使我收到警告msg无法访问的代码int b = 20为什么

它是否在内部声明切换块但没有初始化b)

Hi

Why does C allows declaration of variable inside switch block.
ex: foll prg does not gives "undeclared "b" error msg. but also does
not initialize b to 20
int a=1;
switch(a)
{
int b=20;
case 1: printf("b is %d\n",b);
break;
default:printf("b is %d\n",b);
break;
}

this prints some garbage.
( yeah even though i got warning msg "unreachable code int b = 20" why
is it declaring inside switch block but not initializing b)

推荐答案

" sunny" < su ******* @ gmail.comwrites:
"sunny" <su*******@gmail.comwrites:

>为什么C允许在switch块内声明变量。
ex: foll prg没有给出未声明的b错误消息但也没有把b初始化为20

int a = 1;

开关(a)

{
int b = 20;

案例1:printf(b是%d \ n,b);

休息;

默认值:printf(" b is%d \ n",b);

break;

}
>Why does C allows declaration of variable inside switch block.
ex: foll prg does not gives "undeclared "b" error msg. but also does
not initialize b to 20
int a=1;
switch(a)
{
int b=20;
case 1: printf("b is %d\n",b);
break;
default:printf("b is %d\n",b);
break;
}


>这打印出一些垃圾。
(是的,即使我收到警告msg无法访问的代码int b = 20为什么
是在内部切换块声明但没有初始化b )
>this prints some garbage.
( yeah even though i got warning msg "unreachable code int b = 20" why
is it declaring inside switch block but not initializing b)



程序不会给出关于自己的警告;

不同的编译器可能会给出不同的警告和错误。


你可能会被达夫的装置吓坏:

http://www.lysator.liu.se/c/duffs-de...l#duffs-device
http://www.catb.org/jargon/html/D/Duffs -device.html


-

Chris,


Programs do not give warnings about themselves;
different compilers may give different warnings and errors.

You will probably be horrified by Duff''s Device:

http://www.lysator.liu.se/c/duffs-de...l#duffs-device
http://www.catb.org/jargon/html/D/Duffs-device.html

--
Chris,


sunny写道:
sunny wrote:




为什么C允许在开关块内声明变量。

ex: foll prg没有给出未声明的b错误消息但也确实

没有将b初始化为20

int a = 1;

开关(a)

{

int b = 20;

案例1:printf(b是%d \ n,b);

break;

默认:printf(" b是%d \ nn,b);

休息;

}


打印出一些垃圾。

(是的,即使我收到警告msg无法访问的代码int b = 20为什么

它是否在内部声明切换块但没有初始化b)
Hi

Why does C allows declaration of variable inside switch block.
ex: foll prg does not gives "undeclared "b" error msg. but also does
not initialize b to 20
int a=1;
switch(a)
{
int b=20;
case 1: printf("b is %d\n",b);
break;
default:printf("b is %d\n",b);
break;
}

this prints some garbage.
( yeah even though i got warning msg "unreachable code int b = 20" why
is it declaring inside switch block but not initializing b)



这取决于你的编译器。我正在使用gcc,它会抛出

警告信息


[kokje @RocketFuel~]


It depends on your compiler. I am using gcc and it throws following
warning message

[kokje@RocketFuel ~]


gcc test。 c -o test

test.c:函数`main'':

test.c:7:警告:switch语句开头无法访问的代码


您使用的编译器是什么?


Tejas Kokje

gcc test.c -o test
test.c: In function `main'':
test.c:7: warning: unreachable code at beginning of switch statement

What compiler are you using ?

Tejas Kokje


这篇关于为什么C标准允许这个声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆