机器字长:是指计算机进行一次整数运算所能处理的二进制数据的位数(整数运算即定点整数运算)。机器字长也就是运算器进行定点数运算的字长,通常也是CPU内部数据通路的宽度。现在一般为32位即4个字节,也有64位和16位的。
算术类型的存储空间按照机器而定。一般,short类型为半个机器字长,int为一个机器字长,long为1或2个机器字长,float为一个机器字长,double为两个字,long double用3或4个字长。C++标准规定的是每个算术类型的最小存储空间,但其并不阻止编译器用更大的存储空间。如果要保证移植性,尽量用__int16 __int32 __int64吧,或者自己typedef int INT32一下。
对于具体每种数据类型占用多少字节数,标准中并没有具体给出规定那个基本类型应该是多少字节数,而且这个也与机器、OS、编译器有关,比如同样是在32bits的操作系统系,VC++的编译器下int类型为占4个字节;而tuborC下则是2个字节。
下面的表格汇总了一下,常用的数据类型分别在16位、32位和64位系统下占用的字节数。
[table id=3 /]
同时可以通过代码来查看自己机器上各个数据类型占用的字节数和取值范围。
[cc lang=”C++”]
#include
#include
#include
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************"<< endl;
cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t\t最小值:" << (numeric_limits::min)() << endl;
cout << "char: \t\t" << "所占字节数:" << sizeof(char);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t\t最小值:" << (numeric_limits::min)() << endl;
cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t\t最小值:" << (numeric_limits::min)() << endl;
cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t\t最小值:" << (numeric_limits::min)() << endl;
cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t\t最小值:" << (numeric_limits::min)() << endl;
cout << "short: \t\t" << "所占字节数:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t\t最小值:" << (numeric_limits::min)() << endl;
cout << "int: \t\t" << "所占字节数:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t最小值:" << (numeric_limits::min)() << endl;
cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t最小值:" << (numeric_limits::min)() << endl;
cout << "long: \t\t" << "所占字节数:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t最小值:" << (numeric_limits::min)() << endl;
cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t最小值:" << (numeric_limits::min)() << endl;
cout << "double: \t" << "所占字节数:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t最小值:" << (numeric_limits::min)() << endl;
cout << "long double: \t" << "所占字节数:" << sizeof(long double);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t最小值:" << (numeric_limits::min)() << endl;
cout << "float: \t\t" << "所占字节数:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t最小值:" << (numeric_limits::min)() << endl;
cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
cout << "\t最大值:" << (numeric_limits::max)();
cout << "\t最小值:" << (numeric_limits::min)() << endl;
cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
// << "\t最大值:" << (numeric_limits::max)() << "\t最小值:" << (numeric_limits::min)() << endl;
cout << "type: \t\t" << "************size**************"<< endl;
return 0;
}
[/cc]
运行结果:
