楼主啊,这个不同类型的变量所占的字节数与你的机器有关
但是你可以通过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.
温馨提示:答案为网友推荐,仅供参考