wangdoc / clang-tutorial

C 语言教程

Home Page:https://wangdoc.com/clang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

for循环变量的作用域问题

niaucz opened this issue · comments

变量的作用域
链接文章结尾处的代码

for (int i = 0; i < 5; i++) {
  int i = 999;
  printf("%d\n", i);
}

printf("%d\n", i); // 非法

运行报错 error: redeclaration of 'int i'

image

老师想表达的是嵌套循环的作用域?

for (int i = 0; i < 5; i++)
{
    for (int i = 0; i < 3; i++)
    {
        //...
    }
}

这样是可以重复定义i变量的。
image

你用的不是 GCC 吧。