#include<stdio.h>
int main()
{
switch(2)
{
case 1:
if(1)
{
case 2:
printf("hello\n");
};
}
return 0;
}
输出=你好
因为我在 switch
中传递 2
case 1
不成立,那么它也会进入它并执行 case 2
内的代码。
怎么会进入case 1
呢?
谢谢。
最佳答案
在switch(2)
之后,会立即跳转到case 2
标签。事实上,它位于 case 1
中包含的 if
block 中,这一事实是无关紧要的。 情况 2:
的有效功能与 goto
标签没有什么不同,因此无论它在哪里,它都会跳转到该标签。案例 1 以某种方式被输入是不正确的。
为了澄清,正确缩进它看起来像这样:
#include<stdio.h>
int main() {
switch(2) {
case 1:
if(1) {
case 2:
printf("hello\n");
}
;
}
return 0;
}
关于c - switch case 与失败的 case 中的执行 case 不一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25950160/