变量声明和switch语句初始化 [英] Variable declared and initialized in a switch statement

查看:210
本文介绍了变量声明和switch语句初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个程序不输出20?

 #包括LT&;&stdio.h中GT;诠释主(){
    INT A = 1;
    开关(一){
            INT B = 20;
        情况1:
        {
            的printf(B为%d \\ n,B);
            打破;
        }
        默认:
        {
            的printf(B为%d \\ n,B);
            打破;
        }
    }
    返回0;
}


解决方案

由于switch语句跳转到一个相关的情况下,太行 INT B = 20 将永远执行

Why does this program not output 20?

#include<stdio.h>

int main() {
    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;
        }
    }
    return 0;
}

解决方案

Because the switch statement jumps to a relevant case, so the line int b = 20 will never be executed.

这篇关于变量声明和switch语句初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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