当“default:”标签放在大括号外面时,复合 switch 语句中的局部变量是否会被初始化?

发布于 2024-12-25 12:12:20 字数 980 浏览 2 评论 0原文

通常,当使用 switch 语句时,您不能定义初始化复合语句的局部变量,例如

switch (a)
{
  int b = 5;  /* Initialization is skipped, no matter what a is */

  case 1:
    /* Do something */
    break;
  default:
   /* Do something */
   break;
}

但是,因为 switch 语句是一个语句像 forwhile 一样,没有禁止不使用复合语句的规则,请参阅此处的示例。但这意味着,可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2025-01-01 12:12:20

大多数人将 switch 视为多个 if,但从技术上讲,它是一个经过计算的 gotocase: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 calculated goto. And the case <cte>: and default: are actually labels. So the rules of goto apply in these cases.

Your both your examples are syntactically legal, but in the second one, when a==1 the b 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:

If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文