小编Xat*_*ian的帖子

在同一函数中定义变量及其静态等价物

我不明白以下代码是如何工作的:

#include "stdio.h"

int main(void) {
  int i = 3;
  while(i--) {
    static int i = 100;
    i--,
    printf("%d\n", i);
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用Clang或GCC编译的代码打印以下输出:

99
98
97
Run Code Online (Sandbox Code Playgroud)

有人能解释一下这里发生了什么吗?看起来在一条指令中实现了两次操作并且不止一次.是不确定的行为?我在C++中观察到相同的行为.

c c++ static scope shadowing

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

标签 统计

c ×1

c++ ×1

scope ×1

shadowing ×1

static ×1