#include <iostream>
using namespace std;
int global;
static int staticGlobal;
class test
{
public:
int member;
};
void testFunc()
{
int local;
static int staticLocal;
cout << "local:" << local << endl;
cout << "staticLocal:" << staticLocal << endl;
}
int main()
{
test a;
cout << "globalInt:" << global << endl;
cout << "staticGlobal:" << staticGlobal << endl;
cout << "member:" << a.member << endl;
testFunc();
system("pause");
return 0;
}
![运行结果](https://i-blog.csdnimg.cn/blog_migrate/808ddad14d702300285da43fe1e2c831.png)
总结如下:
- 全局变量、静态全局变量:0
- 类的成员变量、局部变量:无法预测的值
- 静态局部变量:0
- 此外,若不对类的静态成员变量经行初始化,将出现运行时错误
变量的存储位置:
- 全局区(静态区):静态变量、全局变量
- 栈区:局部变量
- 常量区:常量字符串
- 堆区:程序员自己分配释放