变量作用域

#include <iostream>

using namespace std;
float x = 365.5; //声明全局变量;
int main() {
int x = 1, y = 2;
double w = x + y;
{
double x = 1.121, y = 1.362, z = 3.624;
cout << "inner:x=" << x << endl;
cout << "inner:y=" << y << endl;
cout << "inner:z=" << z << endl;
cout << "outer:w=" << w << endl;
cout << "::x=" << ::x << endl; //访问重名的全局变量
cout << "inner:z=" << z << endl;
}
cout << "outer:x=" << x << endl;
cout << "outer:y=" << y << endl;
cout << "outer:w=" << w << endl;

//cout<<"inner:z="<<z<<endl;无效
cout << "::x=" << ::x << endl;    //访问重名的全局变量

return 0;

}

posted @ 2021-07-05 09:19  江南王小帅  阅读(26)  评论(0)    收藏  举报