Does each of C and C++ standards allow sizeof
of numeric types not to be a power of two?
The following constraints are known:
16 <= CHAR_BIT * sizeof(int) <= CHAR_BIT * sizeof(long)
32 <= CHAR_BIT * sizeof(long) <= CHAR_BIT * sizeof(long long)
2 <= sizeof(int) && 4 <= sizeof(long)
Does that mean that sizeof(int) == 3 && sizeof(long) == 5
is a valid behaviour?
If yes - is there any known compiler/architecture behaving in a similar way?
I think 3.9.1/2 (C++98) sums this up nicely (immediately followed by analogous information for the unsigned types):
There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment39) ; the other signed integer types are provided to meet special needs.
Basically all we know is that sizeof(char) == 1
and that each "larger" type is at least that large, with int
being a "natural" size for an architecture (where as far as I can tell "natural" is up to the compiler writer). We don't know anything like CHAR_BIT * sizeof(int) <= 32
etc. Also keep in mind that CHAR_BIT
doesn't have to be 8 either.
It seems fairly safe to say that three byte int
and five byte long
would be allowed for hardware where those sizes were natively used. I am however not aware of any such hardware/architectures.
EDIT: As pointed out in @Nigel Harper comment we do know that int
has to be at least 16 bits and long
at least 32 bits to satisfy range requirements. Otherwise we don't have any specific size restrictions other than as seen above.