C
chrisbazley
Hi,
I have been re-reading section A6.1 'Integral Promotion' of K & R 2nd
edition with reference to section A4.2 'Basic Types'. As an academic
exercise, I am wondering whether a standards-compliant implementation
might exist in which 'int' cannot represent all the values of a
character; in other words, whether a character can ever be promoted to
'unsigned int' during expression evaluation.
This would only happen if SCHAR_MAX > INT_MAX or UCHAR_MAX > INT_MAX.
The former seems highly unlikely because it implies sizeof(signed
char) > sizeof(int). The latter seems more likely because it has the
weaker requirement that sizeof(unsigned char) >= sizeof(int).
The answer to question 1.1 of the comp.lang.c FAQ asserts "something
like the relation sizeof(char) <= sizeof(short) <= sizeof(int) <=
sizeof(long) <= sizeof(long long) is guaranteed" (with a caveat about
storage requirements not necessarily equating to numeric range).
I have often seen similar claims but I can't find such a guarantee in
appendix A of K&R, which merely says "Longer integers provide at least
as much storage as shorter ones" . It doesn't appear to preclude
SCHAR_MAX > INT_MAX. The closest I can find is the guarantee that
"when sizeof is applied to a char, the result is 1". This requires
integer objects to use at least as much storage space as characters
(or else their size couldn't be represented).
Have I missed something?
I have been re-reading section A6.1 'Integral Promotion' of K & R 2nd
edition with reference to section A4.2 'Basic Types'. As an academic
exercise, I am wondering whether a standards-compliant implementation
might exist in which 'int' cannot represent all the values of a
character; in other words, whether a character can ever be promoted to
'unsigned int' during expression evaluation.
This would only happen if SCHAR_MAX > INT_MAX or UCHAR_MAX > INT_MAX.
The former seems highly unlikely because it implies sizeof(signed
char) > sizeof(int). The latter seems more likely because it has the
weaker requirement that sizeof(unsigned char) >= sizeof(int).
The answer to question 1.1 of the comp.lang.c FAQ asserts "something
like the relation sizeof(char) <= sizeof(short) <= sizeof(int) <=
sizeof(long) <= sizeof(long long) is guaranteed" (with a caveat about
storage requirements not necessarily equating to numeric range).
I have often seen similar claims but I can't find such a guarantee in
appendix A of K&R, which merely says "Longer integers provide at least
as much storage as shorter ones" . It doesn't appear to preclude
SCHAR_MAX > INT_MAX. The closest I can find is the guarantee that
"when sizeof is applied to a char, the result is 1". This requires
integer objects to use at least as much storage space as characters
(or else their size couldn't be represented).
Have I missed something?