计算机的内存是以字节byte为单位进行组织的。一个字节是我们能够在c++程序中操作的最小内存单位。
下面我们通过程序打印一下c++中常见的数据类型以及其所能够存储的数据范围。

常见的数据类型可参考下面

Linux C++基本数据类型

 cout << "数据类型   " << "字节数    " << "最小范围:    " << "最大范围:  " << endl;
    cout << "char   " << sizeof (char) << "   " << (numeric_limits<char>::min)() << "  " << (numeric_limits<char>::max)() << endl;
    cout << "int   " << sizeof (int) << "   " << numeric_limits<int>::min() << "  " << numeric_limits<int>::max() << endl;
    cout << "short int   " << sizeof (short int) << "   " << numeric_limits<short int>::min() << "  " << numeric_limits<short int>::max() << endl;
    cout << "long int   " << sizeof (long int) << "   " << numeric_limits<long int>::min() << "  " << numeric_limits<long int>::max() << endl;
    cout << "float   " << sizeof (float) << "   " << numeric_limits<float>::min() << "  " << numeric_limits<float>::max() << endl;
    cout << "double   " << sizeof (double) << "   " << numeric_limits<double>::min() << "  " << numeric_limits<double>::max() << endl;
    cout << "long double   " << sizeof (long double) << "   " << numeric_limits<long double>::min() << "  " << numeric_limits<long double>::max() << endl;
    cout << "bool   " << sizeof (bool) << "   " << numeric_limits<bool>::min() << "  " << numeric_limits<bool>::max() << endl;
    cout << "wchar_t   " << sizeof (wchar_t) << "   " << numeric_limits<wchar_t>::min() << "  " << numeric_limits<wchar_t>::max() << endl;
    cout << "long   " << sizeof (long) << "   " << numeric_limits<long>::min() << "  " << numeric_limits<long>::max() << endl;
    cout << "long long " << sizeof (long long) << "   " << numeric_limits<long long>::min() << "  " << numeric_limits<long long>::max() << endl;
    //无符号数据类型
    cout << "unsigned char   " << sizeof (unsigned char) << "   " << numeric_limits<unsigned char>::min() << "  " << numeric_limits<unsigned char>::max() << endl;
    cout << "unsigned int   " << sizeof (unsigned int) << "   " << numeric_limits<unsigned int>::min() << "  " << numeric_limits<unsigned int>::max() << endl;
    cout << "unsigned long   " << sizeof (unsigned long) << "   " << numeric_limits<unsigned long>::min() << "  " << numeric_limits<unsigned long>::max() << endl;
    cout << "unsigned short   " << sizeof (unsigned short) << "   " << numeric_limits<unsigned short>::min() << "  " << numeric_limits<unsigned short>::max() << endl;
    cout << "unsigned long long   " << sizeof (unsigned long long) << "   " << numeric_limits<unsigned long long>::min() << "  " << numeric_limits<unsigned long long>::max() << endl;
数据类型   字节数    最小范围:    最大范围:  
char   1   �  
int   4   -2147483648  2147483647
short int   2   -32768  32767
long int   8   -9223372036854775808  9223372036854775807
float   4   1.17549e-38  3.40282e+38
double   8   2.22507e-308  1.79769e+308
long double   16   3.3621e-4932  1.18973e+4932
bool   1   0  1
wchar_t   4   -2147483648  2147483647
long   8   -9223372036854775808  9223372036854775807
long long 8   -9223372036854775808  9223372036854775807
unsigned char   1     �
unsigned int   4   0  4294967295
unsigned long   8   0  18446744073709551615
unsigned short   2   0  65535
unsigned long long   8   0  18446744073709551615

上面的结果仅供参考,不同的机器上会有差异存在。

一些基本类型可以使用一个或多个类型修饰符进行修饰:

signed
unsigned
short
long

关于修饰符的更多内容这里就不多说了,不清楚的可以查阅下资料嘚。

相关文章:

  • 2021-07-09
  • 2022-02-06
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
猜你喜欢
  • 2021-10-17
  • 2022-02-18
  • 2021-06-08
相关资源
相似解决方案