#include<iostream>
using namespace std;
int temp = 12;
void test(void );
int main(void )
{
int temp = 13;
cout << "in main: " << temp <<" address: " << endl;
test();
cout << "in main: " << temp << " address": << &temp << endl;
return 0;
}
void test(void )
{
int temp = 14;
cout << "in func: " << temp << " address": << &temp << endl;
{
int temp = 15;
cout << "in func block: " << temp << " address: " << &temp << endl;
}
cout << "in func: " << temp << " address: " << &temp << endl;
}
从结果上面看出,全局变量和局部变量同名的情况下,局部变量会显示,而全局变量会被局部变量隐藏,在局部变量作用完了之后,全局变量才显现出来