c++中各种变量所占字节数

能不能帮忙总结一下c++中各种变量类型所占字节数?
谢谢!

楼主啊,这个不同类型的变量所占的字节数与你的机器有关
但是你可以通过sizeof(类型名)来确定他到底在你的机器上所占用
的字节数,当然类型名不局限于基本类型,还包括很多自定义类型
如类类型了等等,结构类型了,不过那些稍微复杂一点
还可以sizeof(表达式)//表示的是表达式结构所占用的字节数
下面给个程序测试,楼主可以到自己电脑上试试:

#include <iostream>
using namespace std;
int main()
{
cout<<"The size of an int is:\t\t"<<sizeof(int)<<"bytes.\n";
cout<<"The size of a short int is:\t"<<sizeof(short int)<<"bytes.\n";
cout<<"The size of a long int is:\t"<<sizeof(long int)<<"bytes.\n";
cout<<"The size of a char is:\t\t"<<sizeof(char)<<"bytes.\n";
cout<<"The size of a float is:\t\t"<<sizeof(float)<<"bytes.\n";
cout<<"The size of a double is:\t"<<sizeof(double)<<"bytes.\n";
cout<<"The size of a bool is:\t\t"<<sizeof(bool)<<"bytes.\n";
return 0;
}
我电脑上的运行结果显示:
The size of an int is: 4bytes.
The size of a short int is: 2bytes.
The size of a long int is: 4bytes.
The size of a char is: 1bytes.
The size of a float is: 4bytes.
The size of a double is: 8bytes.
The size of a bool is: 1bytes.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-05-09
不同的系统上,所占空间是不同的。
可以用sizeof(类型名)来检查对就类型所占用的字节数量。
第2个回答  2008-05-09
你可以条用sizeof()函数,它就是C++提供的查看变量所占字节数目的函数,其中只有一个参数,可以是某个变量名,也可以是某一个数据类型,例如sizeof(int)和sizeof(a)(当然已经有:int a;)都返回4.
相似回答