I know that the C++ standard explicitly guarantees the size of only char, signed char and unsigned char. Also it gives guarantees that, say, short is at least as big as char, int as big as short etc. But no explicit guarantees about absolute value of, say, sizeof(int). This was the info in my head and I lived happily with it. Some time ago, however, I came across a comment in SO (can’t find it) that in C long is guaranteed to be at least 4 bytes, and that requirement is “inherited” by C++. Is that the case? If so, what other implicit guarantees do we have for the sizes of arithmetic types in C++? Please note that I am absolutely not interested in practical guarantees across different platforms in this question, just theoretical ones.
I know that the C++ standard explicitly guarantees the size of only char ,
Share
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_MAXetc. constants in<limits.h>. For exampleULONG_MAXmust be at least 4294967295, from which we deduce that the width oflongis 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
longis at least 4 bytes, since in C and C++ “byte” is basically defined to mean “char”, and it is not guaranteed thatCHAR_BITis 8 in C or C++.CHAR_BIT == 8is guaranteed by both POSIX and Windows.