C++基本数据类型的大小和取值范围

发布时间 2023-06-27 17:49:41作者: 神奇少女祝XiXi

bit, byte, word

  1. bit 比特,位

Computers store data as a sequence of bits, each holding a 0 or 1, such as 00011011011...

  1. byte 字节

Most computers deal with memory as chunks of bits of sizes that are powers of 2. The smallest chunk of addressable memory is referred to as a "byte".
...
Most computers associate a number (called an "address") with each byte in memory.

  1. word 字
    由若干个字节组成:
    在16位的系统中(比如8086微机) 1字 (word)= 2字节(byte)= 16(bit)
    在32位的系统中(比如win32) 1字(word)= 4字节(byte)=32(bit)
    在64位的系统中(比如win64)1字(word)= 8字节(byte)=64(bit)

C++中的算术类型

  1. C++中的算术类型(arithmetic types)的数据是指可以进行加减乘除等算术运算的数值型数据(也可以理解为就是“数字(number)”)。
  2. 算术类型又可以分为integral types和floating-point types两类:
  • integral types
    • boolean values: bool
    • characters: char ...
    • integers: short, int, long, long long
  • floating-point types
    • float, double, ...

C++中算术类型的尺寸

  1. C++中算术类型的size(尺寸)指的是该类型的数据在内存中所占的字节数。

  2. C++标准中只对每一种类型的minimum size进行了规定,真实尺寸依赖于编译器的具体实现。
    image

  3. char:1 byte

  4. sizeof(xxx)=sizeof(unsigned xxx)=sizeof(signed xxx)

  5. sizeof(short)<=sizeof(int)<=sizeof(long)<=sizeof(long long)

  6. Typically, floats are represented in one word (32 bits), doubles in two words (64 bits), and long doubles in either three or four words (96 or 128 bits). The float and double types typically yield about 7 and 16 significant digits, respectively.

  7. 类型尺寸依赖于编译器的具体实现,相应的,不同类型的取值范围(最大值和最小值)也依赖于编译器的具体实现。

  8. 查看类型尺寸和类型取值范围的代码:

#include <iostream>
using namespace std;

// 查看int类型所占的字节数
cout << "所占字节数:" << sizeof(int);
// 查看int类型的取值范围
cout << "最大值:" << (numeric_limits<int>::max)();
cout << "最小值:" << (numeric_limits<int>::min)() << endl;
  1. 下面是clang编译器分别在32位和64位机器上的数据类型尺寸:

代码:

#include <iostream>
using namespace std;

int main()
{
    cout << "sizeof(bool)          : " << sizeof(bool) << endl;
    cout << "sizeof(char)          : " << sizeof(char) << endl;
    cout << "sizeof(int)           : " << sizeof(int) << endl;
    cout << "sizeof(unsigned int)  : " << sizeof(unsigned int) << endl;
    cout << "sizeof(short int)     : " << sizeof(short int) << endl;
    cout << "sizeof(long int)      : " << sizeof(long int) << endl;
    cout << "sizeof(float)         : " << sizeof(float) << endl;
    cout << "sizeof(double)        : " << sizeof(double) << endl;

    cout << "min(bool)          : " << numeric_limits<bool>::min() << endl;
    cout << "min(int)           : " << numeric_limits<int>::min() << endl;
    cout << "min(unsigned int)  : " << numeric_limits<unsigned int>::min() << endl;
    cout << "min(short int)     : " << numeric_limits<short int>::min() << endl;
    cout << "min(long int)      : " << numeric_limits<long int>::min() << endl;
    cout << "min(float)         : " << numeric_limits<float>::min() << endl;
    cout << "min(double)        : " << numeric_limits<double>::min() << endl;

    cout << "max(bool)          : " << numeric_limits<bool>::max() << endl;
    cout << "max(int)           : " << numeric_limits<int>::max() << endl;
    cout << "max(unsigned int)  : " << numeric_limits<unsigned int>::max() << endl;
    cout << "max(short int)     : " << numeric_limits<short int>::max() << endl;
    cout << "max(long int)      : " << numeric_limits<long int>::max() << endl;
    cout << "max(float)         : " << numeric_limits<float>::max() << endl;
    cout << "max(double)        : " << numeric_limits<double>::max() << endl;

    return 0;
}

运行结果:
image
总结:
image