Maison  >  Questions et réponses  >  le corps du texte

C++循环的重复定义问题


第六行的int i = 0;在每轮循环均会执行,为什么能编译成功而不是提示重复定义?

天蓬老师天蓬老师2713 Il y a quelques jours488

répondre à tous(3)je répondrai

  • PHP中文网

    PHP中文网2017-04-17 14:40:55

    C++的变量作用域问题

    #include <iostream>
    
    using namespace std;
    int main(int argc, char *argv[]) {
        int a = 3;
        int i = 3;
        {
            int i = 1;
        }
        cout << i << endl; // 3
    }

    répondre
    0
  • 怪我咯

    怪我咯2017-04-17 14:40:55

    类似于全局变量和局部变量的概念。

    一个变量只在“{}”中间起作用,出了这个大括号,这个变量就自动删除了。

    répondre
    0
  • 黄舟

    黄舟2017-04-17 14:40:55

    编译器没有那么“笨”,它会知道你的意思是在循环中使用一个被初始化为0的变量。而上边a就不同了,编译器搞不清楚你为什么要把a定义两遍,很可能是误输入,所以会报错。

    répondre
    0
  • Annulerrépondre