//file1
#include <iostream>
using namespace std;
void other();
void another();
int x = 10;
int y;
int main(){
cout << x << endl;
{
int x = 4;
cout << x << endl;
cout << y << endl;
}
other();
another();
return 0;
}
void other(){
int y =1;
cout << "other : " << x <<" " << y;
}
~
//file2
#include <iostream>
using namespace std;
extern int x;
namespace
{
int y = -4;
}
void another()
{
cout << "another():" << x <<" " << y << endl;
}
~
输出结果:
10
4
0
other : 10 1
another():10 -4
1.int 默认值问题:
如果是定义的全局变量或者静态变量(y),未初始化的话就是0.
如果是局部变量,那就是以前残留在堆栈里的随机值。