C++有几种基本的数据类型:char、int、float、double,这些数据类型的字节数、范围大小根据操作系统、编译器的不同而不同。
- 在Windows上,同一种基本数据类型,其win32与win64的字节数是一致的;比如,int在win32、win64都是4个字节。
- 在Linux上,大部分基本数据类型,其32位与64位的字节数是一致的;只有long类型的不相同(当然,long long除外,long long在32位、64位的Linux上,都是8字节)。比如,float在32位、64位的Linux上,都是4个字节;double在32位、64位的Linux上都是8个字节。
当前,在Windows,有些复合数据类型,在不同位数上,其字节数是不同的。比如,string、size_t这类复合数据类型,string在win32上占28个字节,而在win64位上占40个字节。size_t在win32上占4个字节,而在win64位上占8个字节。
1、Windows系统 1.1 32位的编译器32位 Windows上的数据类型与范围 type: ************size************** bool: 所占字节数:1 最大值:1 最小值:0 char: 所占字节数:1 最大值:DEL 最小值:€ signed char: 所占字节数:1 最大值:DEL 最小值:€ unsigned char: 所占字节数:1 最大值:255 最小值:0 wchar_t: 所占字节数:2 最大值:65535 最小值:0 short: 所占字节数:2 最大值:32767 最小值:-32768 int: 所占字节数:4 最大值:2147483647 最小值:-2147483648 unsigned: 所占字节数:4 最大值:4294967295 最小值:0 long: 所占字节数:4 最大值:2147483647 最小值:-2147483648 unsigned long: 所占字节数:4 最大值:4294967295 最小值:0 double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308 long double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308 float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38 size_t: 所占字节数:4 最大值:4294967295 最小值:0 string: 所占字节数:28 type: ************size**************1.2 64位的编译器
64位 Windows上的数据类型与范围 type: ************size************** bool: 所占字节数:1 最大值:1 最小值:0 char: 所占字节数:1 最大值:DEL 最小值:€ signed char: 所占字节数:1 最大值:DEL 最小值:€ unsigned char: 所占字节数:1 最大值:255 最小值:0 wchar_t: 所占字节数:2 最大值:65535 最小值:0 short: 所占字节数:2 最大值:32767 最小值:-32768 int: 所占字节数:4 最大值:2147483647 最小值:-2147483648 unsigned: 所占字节数:4 最大值:4294967295 最小值:0 long: 所占字节数:4 最大值:2147483647 最小值:-2147483648 unsigned long: 所占字节数:4 最大值:4294967295 最小值:0 double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308 long double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308 float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38 size_t: 所占字节数:8 最大值:18446744073709551615 最小值:0 string: 所占字节数:40 type: ************size**************
Windows获取数据类型的字节数、范围大小的案例程序,如下:
//getSize.cpp
#include2、Linux系统 2.1 32位编译器#include using namespace std; int main() { cout << "type: tt" << "************size**************" << endl; cout << "bool: tt" << "所占字节数:" << sizeof(bool); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "char: tt" << "所占字节数:" << sizeof(char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "signed char: t" << "所占字节数:" << sizeof(signed char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned char: t" << "所占字节数:" << sizeof(unsigned char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "wchar_t: t" << "所占字节数:" << sizeof(wchar_t); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "short: tt" << "所占字节数:" << sizeof(short); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "int: tt" << "所占字节数:" << 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: tt" << "所占字节数:" << 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: tt" << "所占字节数:" << 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: tt" << "************size**************" << endl; system("pause"); return 0; }
32位 Linux上的数据类型与范围 type: ************size************** bool: 所占字节数:1 最大值:1 最小值:0 char: 所占字节数:1 最大值:Del 最小值:€ signed char: 所占字节数:1 最大值:Del 最小值:€ unsigned char: 所占字节数:1 最大值:255 最小值:0 wchar_t: 所占字节数:2 最大值:65535 最小值:0 short: 所占字节数:2 最大值:32767 最小值:-32768 int: 所占字节数:4 最大值:2147483647 最小值:-2147483648 unsigned: 所占字节数:4 最大值:4294967295 最小值:0 long: 所占字节数:4 最大值:2147483647 最小值:-2147483648 unsigned long: 所占字节数:4 最大值:4294967295 最小值:0 double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308 long double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308 float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38 size_t: 所占字节数:4 最大值:4294967295 最小值:0 string: 所占字节数:282.2 64位编译器
64位 Linux上的数据类型与范围 type: ************size************** bool: 所占字节数:1 最大值:1 最小值:0 char: 所占字节数:1 最大值:DEL 最小值:€ signed char: 所占字节数:1 最大值:DEL 最小值:€ unsigned char: 所占字节数:1 最大值:255 最小值:0 wchar_t: 所占字节数:2 最大值:65535 最小值:0 short: 所占字节数:2 最大值:32767 最小值:-32768 int: 所占字节数:4 最大值:2147483647 最小值:-2147483648 unsigned: 所占字节数:4 最大值:4294967295 最小值:0 long: 所占字节数:8 最大值:9223372036854775807 最小值:-9223372036854775807 unsigned long: 所占字节数:8 最大值:18446744073709551615 最小值:0 double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308 long double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308 float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38 size_t: 所占字节数:8 最大值:18446744073709551615 最小值:0 string: 所占字节数:40 type: ************size**************



