问题描述
嗨
为什么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)
推荐答案
程序不会给出关于自己的警告;
不同的编译器可能会给出不同的警告和错误。
你可能会被达夫的装置吓坏:
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,
这取决于你的编译器。我正在使用gcc,它会抛出
警告信息
[kokje @RocketFuel~]
It depends on your compiler. I am using gcc and it throws following
warning message
[kokje@RocketFuel ~]
这篇关于为什么C标准允许这个声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!