switch 语句是如何工作的? [英] How does switch statement work?
问题描述
如何处理 switch-case
块中任何 case
标记语句之前的语句.请解释以下程序的行为
程序 1:
#includeint main(){开关(1){int i=0;情况 1:printf("%d",i);}getchar();返回0;}
输出:垃圾值.
程序 2:
#includeint main(){开关(1){printf("内部开关");case 1:printf("Case 1\n");}printf("外部开关");getchar();返回0;}
输出:
案例 1
外部开关.
根据程序 2,标记为 case 的语句之前的语句似乎无法访问,但是为什么我不会在第一个程序中收到未声明变量 i
的错误(仅警告).>
如果有人能详细解释如何在内部处理 switch
语句,那将会非常有帮助.
这最好通过引用 c 标准来解释.我在此处引用适用于您的问题的标准中的相关部分.
6.8.4.2 switch 语句
第 4 段:
<块引用>switch
语句导致控制跳转到、进入或跳过作为switch
主体,取决于控制表达式的值,以及是否存在default
标签和开关体上或中的任何 case
标签的值......
第 2 段:
<块引用>如果一个 switch
语句在一个范围内有一个关联的 case
或 default
标签具有可变修改类型的标识符,整个 switch 语句应在该标识符的范围.154)
脚注:
<块引用>154) 也就是说,声明要么在 switch 语句之前,要么在最后一个与 case
或 default
相关的标签之后code>switch 位于包含声明的块中.
第 7 段:
Example 在人工程序片段中
开关(expr){int i = 4;f(i);案例0:我 = 17;/* 陷入默认代码 */默认:printf("%d\n", i);}
<块引用>
标识符为 i
的对象以自动存储持续时间存在(在块内)但从未初始化,因此如果控制表达式具有非零值,调用 printf
函数将访问一个不确定的值.同样,对f
函数的调用也无法访问.
<小时>
上述内容适用于问题中的两个代码示例.Example 1
, i
有一个 Indeterminate 值,因为它从未被初始化 &因此打印垃圾,而在示例2
,printf
调用未到达,因为控件跳转到匹配的case
标签.
How are statements that come before any case
labelled statement in a switch-case
block treated.
Please explain the behavior of the following programs
prog1:
#include<stdio.h>
int main()
{
switch(1)
{
int i=0;
case 1:printf("%d",i);
}
getchar();
return 0;
}
Output: garbage value.
prog2:
#include<stdio.h>
int main()
{
switch(1)
{
printf("Inside Switch");
case 1:printf("Case 1\n");
}
printf("Outside Switch");
getchar();
return 0;
}
Output:
Case 1
Outside Switch.
The statements before a case labelled statement seem unreachable according to program 2 but then why don't i get an error for an undeclared variable i
in the first program (only a warning).
Would be really helpful if someone could explain in detail that how the switch
statement is treated internally.
This is best explained by quotations from the c standard. I am quoting the relevant parts from the standard which apply to your question here.
6.8.4.2 The switch statement
Para 4:
A
switch
statement causes control to jump to, into, or past the statement that is theswitch
body, depending on the value of a controlling expression, and on the presence of adefault
label and the values of anycase
labels on or in the switch body......
Para 2:
If a
switch
statement has an associatedcase
ordefault
label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.154)
FootNote:
154) That is, the declaration either precedes the switch statement, or it follows the last
case
ordefault
label associated with theswitch
that is in the block containing the declaration.
Para 7:
EXAMPLE In the artificial program fragment
switch (expr)
{
int i = 4;
f(i);
case 0:
i = 17;
/* falls through into default code */
default:
printf("%d\n", i);
}
the object whose identifier is
i
exists with automatic storage duration (within the block) but is never initialized, and thus if the controlling expression has a nonzero value, the call to theprintf
function will access an indeterminate value. Similarly, the call to the functionf
cannot be reached.
The above mentioned applies to both of the code examples in the Question.
Example 1
, i
has an Indeterminate value since it was never initialized & hence prints garbage, While in
Example 2
, printf
call is not reached because the control jumps to the matching case
label.
这篇关于switch 语句是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!