开关语句中变量定义 [英] Variable definition inside switch statement

查看:276
本文介绍了开关语句中变量定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下code,为什么变量 I 未分配值 1

In the following code, why is the variable i not assigned the value 1?

  int main()
  { 
      int val = 0;
      switch (val) {         
          int i = 1;   //i is defined here

          case 0:
              printf("value:%d\n",i);
              break;
          default:
              printf("value:%d\n",i);
              break;
      }
      return 0;
  }

$gcc -Wall test.c
warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
    printf("value%d\n",i);
    ^

如果 VAL = 0 ,然后输出 0

如果 VAL = 1 或其他任何东西,那么输出也为0。

If val=1 or anything else, then the output is also 0.

为什么变量 I 声明,但开关内部没有定义,请给我解释一下。对象标识符为我存在与(块内)自动存储时间,但永远不会初始化。为什么呢?

Please explain to me why the variable i is declared but not defined inside the switch. The object whose identifier is i exists with automatic storage duration (within the block) but is never initialized. Why?

推荐答案

据C标准(6.8报表和块),重点煤矿:

According to the C standard (6.8 Statements and blocks), emphasis mine:

3块允许一系列的声明和语句进行分组
  在一个语法单位。 的对象有初始化
  自动存储时间,和变长数组声明符
  带座普通范围的标识符,进行评估和值
  存储在对象(包括存储一个不确定的值
  在没有初始化的对象)的每一次的声明
  在执行顺序到达,就好像它是一个声明,

  在声明符出现的顺序每个声明中。

3 A block allows a set of declarations and statements to be grouped into one syntactic unit. The initializers of objects that have automatic storage duration, and the variable length array declarators of ordinary identifiers with block scope, are evaluated and the values are stored in the objects (including storing an indeterminate value in objects without an initializer) each time the declaration is reached in the order of execution, as if it were a statement, and within each declaration in the order that declarators appear.

和(6.8.4.2 switch语句)

And (6.8.4.2 The switch statement)

4开关语句导致控制的跳到,进入或经过
  声明是开关体,取决于a的值
  控制前pression,并在默认标签的presence和
  任何情况下,值标签上或者在开关本体。 case或default
  标签仅在最近的封闭开关进行访问
  声明。

4 A switch statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a default label and the values of any case labels on or in the switch body. A case or default label is accessible only within the closest enclosing switch statement.

因此​​变量的初始化I 不会求,因为声明

Thus the initializer of variable i is never evaluated because the declaration

  switch (val) {         
      int i = 1;   //i is defined here
      //...

不执行的顺序因跳转到case标签,并想与自动存储时间的变量有不确定的值达到了。

is not reached in the order of execution due to jumps to case labels and like any variable with the automatic storage duration has indeterminate value.

也是这种规范性示例,请参见从6.8.4.2/7:

See also this normative example from 6.8.4.2/7:

例如,在人工程序片段

switch (expr) 
{ 
    int i = 4;
    f(i); 

case 0: 
    i = 17; /* falls through into default code */ 
default:
    printf("%d\n", i); 
}


  
  

其标识符的对象是的与存在
  自动存储时间(块中),但从来没有
  初始化,并因此,如果控制前pression具有非零
  值,调用printf函数将进入一个不确定
  值。同样,调用函数f无法达成。

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 the printf function will access an indeterminate value. Similarly, the call to the function f cannot be reached.

这篇关于开关语句中变量定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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