On Tue, 31 Jan 2012, Chuck Guzis wrote:
Sort of. Are there any particular C rules saying that
types long,
int and char must be integral multiples of one another. For
instance, could one have a char of 16 bits, an int of 21 bits and a
long of 29 bits?
On 01/31/2012 09:48 PM, Fred Cisin wrote:
Absolutely.
Not in ISO C. All types have to be
a multiple of the size of the char
type, which must be at least 8 bits. You could have 8-bit char, 24 bit
short, 40 bit int, and 48 bit long. You can't have a 21 bit int, unless
the char type is 21 bits, because 21 doesn't have any factor n such that
8 <= n < 21.
In K&R C (I don't know from ANSI), it was
recommended that an int be
whatever type was easiest to deal with.
ISO C section 6.2.5 paragraph 5 says about
the int type:
A ??plain?? int object has the natural size suggested by the
architecture of the execution environment (large enough to contain any
value in the range INT_MIN to INT_MAX as defined in the header <limits.h>).
A short int could be the same or smaller.
Section 6.2.5 paragraph 8 and section 6.3.1.1 together require the sizes
to obey the relationship:
1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <=
sizeof(long long)
Section 6.2.5 paragraph 6 requires that the unsigned types have the same
storage and alignment requirements as the corresponding signed types, so
the above relationship also holds for the unsigned types.
I just learned, somewhat to my surprise, that ISO C *requires* that
there be an integer type of at least 64 bits. This requirement is in
sections 7.18.1.2 and 7.18.1.3, on minimum-width integer types and
fastest minimum-width integer types, which require an implementation to
provide the int_least64_t, uint_least64_t, int_fast64_t, and
uint_fast64_t types, and in section 7.18.2.5, which gives requirements
for INTMAX_MIN, INTMAX_MAX, and UINTMAX_MAX based on having at least 64
bits. I wonder how many C implementations fail to be standard-conformant
due to this requirement?