C++循环的重复定义问题
天蓬老师
天蓬老师 2017-04-17 14:38:55
0
3
556


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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
洪涛

C++ variable scope problem

#include <iostream>

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

Similar to the concept of global variables and local variables.

A variable only works in the middle of "{}". Once the curly brackets are released, the variable will be automatically deleted.

黄舟

The compiler is not that "dumb" and will know that you mean to use a variable that is initialized to 0 in the loop. The above a is different. The compiler can't figure out why you defined a twice. It is probably a wrong input, so an error will be reported.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template