c switch 语句

标签 c switch-statement

void display()
{
    printf("every thing is ok");
}
void main()
{
    int ch;
    while(1)
    {
        printf("enter your choice");
        scanf("%d",&ch);
        switch(ch)

        {
            case 1: clrscr();printf("when choice is one every thing is fine");
                    display();
                    break;
            case 2: clrscr();printf("when chice is two its confusing");
                    display();
                    break;
            case 3: exit(0);
            default: printf("enter choice as 1 or 2 or to exit enter 3");
        }
    }
}

当我跟踪这个 C 程序并输入选项 2 时,它会从 case 1 block 调用显示函数。我不明白。请回复并解释一下。我真的很困惑。

最佳答案

编译器可能正在重新排列您的源语句,从而折叠其 basic blocks 。然后,调试器会将这两种情况下对 display() 的调用匹配到相同的源代码行号。当启用优化时,这是常见的情况。

关于c switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4327869/

相关文章:

c++ - 为什么这个makefile在 'make clean'上执行一个目标

Java 切换案例 : with or without braces?

java - Arraylist - 使用 switch 语句添加字符串

c++ - 如何循环 switch 语句直到找到所需的数字?

if-statement - 我该如何解决?我在程序中解释

java - 使用用户输入的子字符串的 switch 语句

c - 如何通过#define 指令检测 LLVM 及其版本?

java - 使用 C 结构的自定义创建器和删除器

c - realloc 和 malloc 函数

c - 扫描文件以获取签名(使用 fread)