As I\'ve learned recently, a long
in C/C++ is the same length as an int
. To put it simply, why? It seems almost pointless to even include the dat
The specific guarantees are as follows:
char
is at least 8 bits (1 byte by definition, however many bits it is)short
is at least 16 bitsint
is at least 16 bitslong
is at least 32 bitslong long
(in versions of the language that support it) is at least 64 bitsThus it makes sense to use long
if you need a type that's at least 32 bits, int
if you need a type that's reasonably fast and at least 16 bits.
Actually, at least in C, these lower bounds are expressed in terms of ranges, not sizes. For example, the language requires that INT_MIN <= -32767
, and INT_MAX >= +32767
. The 16-bit requirements follows from this and from the requirement that integers are represented in binary.
C99 adds
and
, which define types such as uint32_t
, int_least32_t
, and int_fast16_t
; these are typedefs, usually defined as aliases for the predefined types.
(There isn't necessarily a direct relationship between size and range. An implementation could make int
32 bits, but with a range of only, say, -2**23 .. +2^23-1
, with the other 8 bits (called padding bits) not contributing to the value. It's theoretically possible (but practically highly unlikely) that int
could be larger than long
, as long as long
has at least as wide a range as int
. In practice, few modern systems use padding bits, or even representations other than 2's-complement, but the standard still permits such oddities. You're more likely to encounter exotic features in embedded systems.)