如果我不写开关的情况下默认? [英] What if i dont write default in switch case?
问题描述
int a=10;
switch(a)
{
case 0: printf("case 0");
break;
case 1: printf("case 1");
break;
}
以上code是有效的?
看看我非常确信int类型的不会有任何价值,而不是1和0,那么在这种情况下我能避免违约:
如果在任何情况下值将被不同,那么1和0?
What if in any case a value will be differ then 1 and 0 ?
编辑:我知道这是愚蠢的问题,但我想,也许这将是非法的或不确定的行为,因此只是要求,以确保
推荐答案
在code是有效的。如果没有默认值:
标签,没有情况
标签相匹配的转换的值,那么所有的控制复合语句将被执行。执行将继续从switch语句的结束。
The code is valid. If there is no default:
label and none of the case
labels match the "switched" value, then none of the controlled compound statement will be executed. Execution will continue from the end of the switch statement.
ISO / IEC 9899:1999,部分6.8.4.2:
ISO/IEC 9899:1999, section 6.8.4.2:
[...]如果没有转换情况
恒前pression比赛和没有默认
标签,无开关本体的一部分被执行。
[...] If no converted
case
constant expression matches and there is nodefault
label, no part of the switch body is executed.
这篇关于如果我不写开关的情况下默认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!