What does the C++ standard state the size of int, long type to be?

前端 未结 24 2292
无人及你
无人及你 2020-11-21 04:42

I\'m looking for detailed information regarding the size of basic C++ types. I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler.

24条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 04:47

    I notice that all the other answers here have focused almost exclusively on integral types, while the questioner also asked about floating-points.

    I don't think the C++ standard requires it, but compilers for the most common platforms these days generally follow the IEEE754 standard for their floating-point numbers. This standard specifies four types of binary floating-point (as well as some BCD formats, which I've never seen support for in C++ compilers):

    • Half precision (binary16) - 11-bit significand, exponent range -14 to 15
    • Single precision (binary32) - 24-bit significand, exponent range -126 to 127
    • Double precision (binary64) - 53-bit significand, exponent range -1022 to 1023
    • Quadruple precision (binary128) - 113-bit significand, exponent range -16382 to 16383

    How does this map onto C++ types, then? Generally the float uses single precision; thus, sizeof(float) = 4. Then double uses double precision (I believe that's the source of the name double), and long double may be either double or quadruple precision (it's quadruple on my system, but on 32-bit systems it may be double). I don't know of any compilers that offer half precision floating-points.

    In summary, this is the usual:

    • sizeof(float) = 4
    • sizeof(double) = 8
    • sizeof(long double) = 8 or 16

提交回复
热议问题