小编Shu*_*tra的帖子

嵌套范围变量的异常行为

我熟悉C中的嵌套范围规则,其中嵌套块内的相同变量名称会影响具有相同名称的外部变量.但是对于以下代码片段,我无法确定输出的解释.

#include <stdio.h>
int main()
{
    int x = 1, y = 2, z = 3;
    printf(" x = %d, y = %d, z = %d \n", x, y, z);
    {
        int x = 10;
        float y = 20;

        printf(" x = %d, y = %f, z = %d \n", x, y, z);
    }

    {
        int z = 100;
        printf(" x = %d, y = %f, z = %d \n", x, y, z);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上述代码段的输出是:

x = …

c gcc scope

2
推荐指数
2
解决办法
181
查看次数

标签 统计

c ×1

gcc ×1

scope ×1