C {} Declare local variables inside and outside, address and value use
Code and running results
Look at the code first:
#include <iostream>
using namespace std;
int main()
{
int i = 5;
int j = 6;
cout << "outer i address is : "<< &i << endl;
cout << "outer j address is : "<< &j << endl;
cout << "out1 i+j = " <<i+j << endl;
for(int i = 0;i<5;i++)
{
cout << "inner i address & value is : " << &i << "------"<<i << endl;
cout <<"inner1 i+j = " <<i+j << endl;
for(int j = 4;j<5;j++)
{
cout << "inner j address & value is : " << &j << "------"<<j << endl;
cout << "inner2 i+j = " <<i+j << endl;
}
}
cout << "out2 i+j = " <<i+j << endl;
cout << "******************************************************************" << endl;
return 0;
}
The above is mainly to see the change and use of the address and value of ij after repeated declaration inside and outside the for loop.
Look at the result
in conclusion
1. The variable name is the same, but the address is different;
2. The internal variable is used first, and the assignment has no effect on the external.