Why are C++ int and long types both 4 bytes?

前端 未结 8 1016
栀梦
栀梦 2020-12-01 10:49

Many sources, including Microsoft, reference both the int and long type as being 4 bytes and having a range of (signed) -2,147,483,648 to 2,147,483,647. What is the point of

相关标签:
8条回答
  • 2020-12-01 11:45

    As other have pointed out, the assumption underlying the question is only partially true, i.e. doesn't hold for some platforms. If you really want to understand how we came to the current situation, the Long Road to 64 Bits by J. Mashey will give you a good view on the various forces in presence and how they interacted.

    Quick summary, C started with char (8 bits) and int (16 bits). Then one added short (16 bits) and long (32 bits), while int could be 16 or 32 bits depending on what was natural on the platform and the pressure of backward compatibility. When 64 bits came, long long was added as a 64 bits type and there was some adjustments in the smaller types on 64 bits platforms. Things stabilized with a 32 bits int, but long kept various definitions, some 64 bits platforms having a 64 bits long, while other (perhaps only Windows?) kept long to 32 bits, probably due to different pressure of backward compatibility (Unix had a long history of assuming long as the same size as a pointer, Windows had more remnant in its API of the time when int was 16 bits and thus long was the only 32 bits type). BTW typedefs (intXX_t, intptr_t) were added to help to make the intention clearer, at the risk for the intXX_t family to force constant size were none is really needed.

    0 讨论(0)
  • 2020-12-01 11:50

    No, it's only 4 bytes on certain platforms. The C++ standard leaves the size as being implementation-defined.

    On other platforms it may be a different size.

    0 讨论(0)
提交回复
热议问题