I know that the C++ standard explicitly guarantees the size of only char
, signed char
and unsigned char
. Also it gives guarantees that
Yes, C++ type sizes are inherited from C89.
I can't find the specification right now. But it's in the Bible.
The C standard do not explicitly say that long
has to be at least 4 bytes, but they do specify a minimum range for the different integral types, which implies a minimum size.
For example, the minimum range of an unsigned long
is 0 to 4,294,967,295. You need at least 32 bits to represent every single number in that range. So yes, the standard guarantee (indirectly) that a long
is at least 32 bits.
C++ inherits the data types from C, so you have to go look at the C standard. The C++ standard actually references to parts of the C standard in this case.
18.2.2 guarantees that <climits>
has the same contents as the C library header <limits.h>
.
The ISO C90 standard is tricky to get hold of, which is a shame considering that C++ relies on it, but the section "Numerical limits" (numbered 2.2.4.2 in a random draft I tracked down on one occasion and have lying around) gives minimum values for the INT_MAX
etc. constants in <limits.h>
. For example ULONG_MAX
must be at least 4294967295, from which we deduce that the width of long
is at least 32 bits.
There are similar restrictions in the C99 standard, but of course those aren't the ones referenced by C++03.
This does not guarantee that long
is at least 4 bytes, since in C and C++ "byte" is basically defined to mean "char", and it is not guaranteed that CHAR_BIT
is 8 in C or C++. CHAR_BIT == 8
is guaranteed by both POSIX and Windows.
Be aware that the guaranteed ranges of these types are one less wide than on most machines:
signed char -127 ... +127 guranteed but most twos complement machines have -128 ... + 127
Likewise for the larger types.
Just be careful about the fact that some machines have chars that are more than 8 bits. For example, IIRC on the TI C5x, a long is 32 bits, but sizeof(long)==2 because chars, shorts and ints are all 16 bits with sizeof(char)==1.
Don't know about C++. In C you have
Annex E (informative) Implementation limits [#1] The contents of the header are given below, in alphabetical order. The minimum magnitudes shown shall be replaced by implementation-defined magnitudes with the same sign. The values shall all be constant expressions suitable for use in #if preprocessing directives. The components are described further in 5.2.4.2.1. #define CHAR_BIT 8 #define CHAR_MAX UCHAR_MAX or SCHAR_MAX #define CHAR_MIN 0 or SCHAR_MIN #define INT_MAX +32767 #define INT_MIN -32767 #define LONG_MAX +2147483647 #define LONG_MIN -2147483647 #define LLONG_MAX +9223372036854775807 #define LLONG_MIN -9223372036854775807 #define MB_LEN_MAX 1 #define SCHAR_MAX +127 #define SCHAR_MIN -127 #define SHRT_MAX +32767 #define SHRT_MIN -32767 #define UCHAR_MAX 255 #define USHRT_MAX 65535 #define UINT_MAX 65535 #define ULONG_MAX 4294967295 #define ULLONG_MAX 18446744073709551615
So char
<= short
<= int
<= long
<= long long
and
CHAR_BIT * sizeof (char) >= 8
CHAR_BIT * sizeof (short) >= 16
CHAR_BIT * size of (int) >= 16
CHAR_BIT * sizeof (long) >= 32
CHAR_BIT * sizeof (long long) >= 64