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

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

问题描述

在下面的代码中,为什么变量i没有赋值1?

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

#include <stdio.h>      

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

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

当我编译时,我收到关于 i 未初始化的警告,尽管 int i = 1; 清楚地初始化了它

When I compile, I get a warning about i not being initialized despite int i = 1; that clearly initializes it

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

如果 val = 0,则输出为 0.

如果 val = 1 或其他任何值,则输出也是 0.

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

请向我解释为什么变量 i 被声明但未在开关内定义.标识符为 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.

And(6.8.4.2 switch 语句)

And (6.8.4.2 The switch statement)

4 switch 语句使控件跳转到、进入或越过作为 switch 主体的语句,取决于 a 的值控制表达式,并在存在默认标签和开关体上或开关体中的任何大小写标签的值.案例或违约标签只能在最近的封闭开关内访问声明.

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
      //...

由于跳转到案例标签,执行顺序未达到,并且与任何具有自动存储持续时间的变量一样具有不确定的值.

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:

EXAMPLE 在仿真程序片段中

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
", i); 
}

标识符为 i 的对象存在于自动存储持续时间(在块内),但从不初始化,因此如果控制表达式有一个非零值,对 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.

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

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