当“default:”标签放在大括号外面时,复合 switch 语句中的局部变量是否会被初始化?
通常,当使用 switch
语句时,您不能定义和初始化复合语句的局部变量,例如
switch (a)
{
int b = 5; /* Initialization is skipped, no matter what a is */
case 1:
/* Do something */
break;
default:
/* Do something */
break;
}
但是,因为 switch
语句是一个语句像 for
或 while
一样,没有禁止不使用复合语句的规则,请参阅此处的示例。但这意味着,可以在 switch
关键字后面的右括号和左大括号之间使用标签。
因此,在我看来,可以并允许使用这样的 switch
语句:
switch (a)
default:
{
int b = 5; /* Is the initialization skipped when a != 1? */
/* Do something for the default case using 'b' */
break;
case 1: // if a == 1, then the initialization of b is skipped.
/* Do something */
break;
}
我的问题:在这种情况下是否必须执行初始化(a!= 1)?据我所知的标准,是的,应该是这样,但我无法在我现有的任何文档中直接找到它。谁能给出一个确定的答案?
在我收到这样的评论之前,是的,我知道这不是现实世界中的编程方式。但是,一如既往,我对语言规范的边界感兴趣。我绝不会容忍我的编程团队出现这种风格!
Normally when using a switch
statement, you cannot define and initialize variables local to the compound statement, like
switch (a)
{
int b = 5; /* Initialization is skipped, no matter what a is */
case 1:
/* Do something */
break;
default:
/* Do something */
break;
}
However, since the switch
statement is a statement like for
or while
, there is no rule against not using a compound statement, look here for examples. But this would mean, that a label may be used between the closing parenthesis after the switch
keyword and the opening brace.
So in my opinion, it would be possible and allowed to use a switch
statement like this:
switch (a)
default:
{
int b = 5; /* Is the initialization skipped when a != 1? */
/* Do something for the default case using 'b' */
break;
case 1: // if a == 1, then the initialization of b is skipped.
/* Do something */
break;
}
My question: Is the initialization necessarily performed in this case (a != 1)? From what I know of the standards, yes, it should be, but I cannot find it directly in any of the documents I have available. Can anyone provide a conclusive answer?
And before I get comments to that effect, yes, I know this is not a way to program in the real world. But, as always, I'm interested in the boundaries of the language specification. I'd never tolerate such a style in my programming team!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数人将
switch
视为多个 if,但从技术上讲,它是一个经过计算的goto
。case:
和default:
实际上是标签。因此,goto
的规则适用于这些情况。您的两个示例在语法上都是合法的,但在第二个示例中,当
a==1
时,将跳过b
初始化,并且其值将是未定义的。只要不使用就没有问题。参考:
根据C99标准,6.2.4.5,关于自动变量:
因此,每次执行流程到达初始化时都会初始化该变量,就像赋值一样。如果您第一次跳过初始化,则该变量将保持未初始化状态。
Most people think of a
switch
as a mutiple if, but it is technically a calculatedgoto
. And thecase <cte>:
anddefault:
are actually labels. So the rules ofgoto
apply in these cases.Your both your examples are syntactically legal, but in the second one, when
a==1
theb
initialization will be skipped and its value will be undefined. No problem as long as you don't use it.REFERENCE:
According to C99 standard, 6.2.4.5, regarding automatic variables:
So the variable is initialized each time the execution flow reaches the initialization, just as it were an assignment. And if you jump over the initialization the first time, then the variable is left uninitialized.