PHP中文网2017-04-17 14:40:55
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
}
怪我咯2017-04-17 14:40:55
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.
黄舟2017-04-17 14:40:55
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.