常见的数据范围
一、总结
| 名称 | 字节 | 位数(二进制) | 最小值 | 最大值 | 位数(十进制) |
| bool | 1 | 8 | 0 | 1 | 1 |
| char | 1 | 8 | |||
| shrot | 2 | 16 (-2^15 到2^15 -1) | -32768 | 32767 | 5 |
| int | 4 | 32 (-2^31 到 2^31 -1) | -2147483648 | 2147483647 | 10 |
| unsigned int | 4 | 32 | 0 | 4294967295 | 10 |
| long | 4 | 32 | -2147483648 | 2147483647 | 10 |
| long long | 8 | 64 | -9223372036854775808 | 9223372036854775807 | 19 |
| float | 4 | 32 | 1.17549e-038 | 3.40282e+038 | |
| double | 8 | 64 | 2.22507e-308 | 1.79769e+308 | |
| string | 8 | 64 |
二、测试代码
#include<iostream>
#include<string>
#include <limits>
using namespace std;
int main()
{
cout << "type:\t\t" << "---size---" << endl;
cout << "bool:\t\t" << "所占字节数:" << sizeof(bool)
<< "\t位数:" << sizeof(bool)*8
<< "\t\t最小值:" << (numeric_limits<bool>::min)()
<< "\t\t最大值:" << (numeric_limits<bool>::max)() << endl << endl;
cout << "char:\t\t" << "所占字节数:" << sizeof(char)
<< "\t位数:" << sizeof(char) * 8
<< "\t\t最小值:" << (numeric_limits<char>::min)()
<< "\t\t最大值:" << (numeric_limits<char>::max)() << endl << endl;
cout << "signed char:\t" << "所占字节数:" << sizeof(signed char)
<< "\t位数:" << sizeof(signed char) * 8
<< "\t\t最小值:" << (numeric_limits<signed char>::min)()
<< "\t\t最大值:" << (numeric_limits<signed char>::max)() << endl << endl;
cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char)
<< "\t位数:" << sizeof(unsigned char) * 8
<< "\t\t最小值:" << (numeric_limits<unsigned char>::min)()
<< "\t\t最大值:" << (numeric_limits<unsigned char>::max)() << endl << endl;
cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t)
<< "\t位数:" << sizeof(wchar_t) * 8
<< "\t最小值:" << (numeric_limits<wchar_t>::min)()
<< "\t\t最大值:" << (numeric_limits<wchar_t>::max)() << endl << endl;
cout << "short: \t\t" << "所占字节数:" << sizeof(short)
<< "\t位数:" << sizeof(short) * 8
<< "\t最小值:" << (numeric_limits<short>::min)()
<< "\t\t最大值:" << (numeric_limits<short>::max)() << endl << endl;
cout << "int: \t\t" << "所占字节数:" << sizeof(int)
<< "\t位数:" << sizeof(int) * 8
<< "\t最小值:" << (numeric_limits<int>::min)()
<< "\t最大值:" << (numeric_limits<int>::max)() << endl << endl;
cout << "unsigned int: \t" << "所占字节数:" << sizeof(unsigned)
<< "\t位数:" << sizeof(unsigned) * 8
<< "\t最小值:" << (numeric_limits<unsigned>::min)()
<< "\t\t最大值:" << (numeric_limits<unsigned>::max)() << endl << endl;
cout << "long: \t\t" << "所占字节数:" << sizeof(long)
<< "\t位数:" << sizeof(long) * 8
<< "\t最小值:" << (numeric_limits<long>::min)()
<< "\t最大值:" << (numeric_limits<long>::max)() << endl << endl;
cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long)
<< "\t位数:" << sizeof(unsigned long) * 8
<< "\t最小值:" << (numeric_limits<unsigned long>::min)()
<< "\t\t最大值:" << (numeric_limits<unsigned long>::max)() << endl << endl;
cout << "float: \t\t" << "所占字节数:" << sizeof(float)
<< "\t位数:" << sizeof(float) * 8
<< "\t最小值:" << (numeric_limits<float>::min)()
<< "\t最大值:" << (numeric_limits<float>::max)() << endl << endl;
cout << "double: \t" << "所占字节数:" << sizeof(double)
<< "\t位数:" << sizeof(double) * 8
<< "\t最小值:" << (numeric_limits<double>::min)()
<< "\t最大值:" << (numeric_limits<double>::max)() << endl << endl;
cout << "size_t: \t" << "所占字节数:" << sizeof(size_t)
<< "\t位数:" << sizeof(size_t) * 8
<< "\t最小值:" << (numeric_limits<size_t>::min)()
<< "\t\t最大值:" << (numeric_limits<size_t>::max)() << endl << endl;
cout << "string: \t" << "所占字节数:" << sizeof(string)
<< "\t位数:" << sizeof(string) * 8 << endl;
cout << "long long: \t" << "所占字节数:" << sizeof(long long)
<< "\t位数:" << sizeof(long long) * 8
<< "\t最小值:" << (numeric_limits<long long>::min)()
<< "\t\t最大值:" << (numeric_limits<long long>::max)() << endl << endl;
system("pause");
return 0;
}扫描二维码推送至手机访问。
版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。



