gpt4 book ai didi

c - switch case 不正确,它的执行 case 在失败的 case 中

转载 作者:太空宇宙 更新时间:2023-11-03 23:26:33 26 4
gpt4 key购买 nike

#include<stdio.h>
int main()
{
switch(2)
{
case 1:
if(1)
{
case 2:
printf("hello\n");
};
}
return 0;
}

输出 = 你好当我在 switch 中传递 2case 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com