【C++】(int)变量初始化的默认值

#include <iostream>
using namespace std;
int global;
static int staticGlobal;
class test
{
public:
	int member;
	//static int staticMember;
};
//int test::staticMember = 0;
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;
	//cout << "staticMember:" << a.staticMember << endl;
	testFunc();
	system("pause");
	return 0;
}

运行结果

总结如下:

  • 全局变量、静态全局变量:0
  • 类的成员变量、局部变量:无法预测的值
  • 静态局部变量:0
  • 此外,若不对类的静态成员变量经行初始化,将出现运行时错误

变量的存储位置:

  • 全局区(静态区):静态变量、全局变量
  • 栈区:局部变量
  • 常量区:常量字符串
  • 堆区:程序员自己分配释放
  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值