各种数据类型的大小由操作系统和编译系统决定
输出您电脑上各种数据类型的大小的代码(C++):
1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 cout << "type: " << "************size**************" << endl; 8 cout << "bool: " << "所占字节数:" << sizeof(bool); 9 cout << " 最大值:" << (numeric_limits<bool>::max)(); 10 cout << " 最小值:" << (numeric_limits<bool>::min)() << endl; 11 cout << "char: " << "所占字节数:" << sizeof(char); 12 cout << " 最大值:" << (numeric_limits<char>::max)(); 13 cout << " 最小值:" << (numeric_limits<char>::min)() << endl; 14 cout << "signed char: " << "所占字节数:" << sizeof(signed char); 15 cout << " 最大值:" << (numeric_limits<signed char>::max)(); 16 cout << " 最小值:" << (numeric_limits<signed char>::min)() << endl; 17 cout << "unsigned char: " << "所占字节数:" << sizeof(unsigned char); 18 cout << " 最大值:" << (numeric_limits<unsigned char>::max)(); 19 cout << " 最小值:" << (numeric_limits<unsigned char>::min)() << endl; 20 cout << "wchar_t: " << "所占字节数:" << sizeof(wchar_t); 21 cout << " 最大值:" << (numeric_limits<wchar_t>::max)(); 22 cout << " 最小值:" << (numeric_limits<wchar_t>::min)() << endl; 23 cout << "short: " << "所占字节数:" << sizeof(short); 24 cout << " 最大值:" << (numeric_limits<short>::max)(); 25 cout << " 最小值:" << (numeric_limits<short>::min)() << endl; 26 cout << "int: " << "所占字节数:" << sizeof(int); 27 cout << " 最大值:" << (numeric_limits<int>::max)(); 28 cout << " 最小值:" << (numeric_limits<int>::min)() << endl; 29 cout << "unsigned: " << "所占字节数:" << sizeof(unsigned); 30 cout << " 最大值:" << (numeric_limits<unsigned>::max)(); 31 cout << " 最小值:" << (numeric_limits<unsigned>::min)() << endl; 32 cout << "long: " << "所占字节数:" << sizeof(long); 33 cout << " 最大值:" << (numeric_limits<long>::max)(); 34 cout << " 最小值:" << (numeric_limits<long>::min)() << endl; 35 cout << "unsigned long: " << "所占字节数:" << sizeof(unsigned long); 36 cout << " 最大值:" << (numeric_limits<unsigned long>::max)(); 37 cout << " 最小值:" << (numeric_limits<unsigned long>::min)() << endl; 38 cout << "double: " << "所占字节数:" << sizeof(double); 39 cout << " 最大值:" << (numeric_limits<double>::max)(); 40 cout << " 最小值:" << (numeric_limits<double>::min)() << endl; 41 cout << "long double: " << "所占字节数:" << sizeof(long double); 42 cout << " 最大值:" << (numeric_limits<long double>::max)(); 43 cout << " 最小值:" << (numeric_limits<long double>::min)() << endl; 44 cout << "float: " << "所占字节数:" << sizeof(float); 45 cout << " 最大值:" << (numeric_limits<float>::max)(); 46 cout << " 最小值:" << (numeric_limits<float>::min)() << endl; 47 cout << "size_t: " << "所占字节数:" << sizeof(size_t); 48 cout << " 最大值:" << (numeric_limits<size_t>::max)(); 49 cout << " 最小值:" << (numeric_limits<size_t>::min)() << endl; 50 cout << "string: " << "所占字节数:" << sizeof(string) << endl; 51 // << " 最大值:" << (numeric_limits<string>::max)() << " 最小值:" << (numeric_limits<string>::min)() << endl; 52 cout << "type: " << "************size**************" << endl; 53 return 0; 54 }
引用:https://www.runoob.com/cplusplus/cpp-data-types.html