semantics of arith comparisons

Stephen Clamage steve at taumet.com
Fri Feb 8 04:23:58 AEST 1991


I wrote:

>Hex constants are of type unsigned int ...

This is not correct, as Andy Koenig and Doug Gwyn pointed out.

In ANSI C (3.1.3.2), the type of a hex constant (without L or U suffix)
is the first in this list in which its value can be represented:
int, unsigned int, long int, unsigned long int.

In pre-ANSI C, a the type of a hex constant (without an L suffix) was int
if it would fit in an unsigned int, and long otherwise.  This led to some
bizarre consequences:  on a system with 16-bit ints, 0xFFFF had type int
and the value -1, while 65535 had type long and value 65535.

The ANSI rules give the more natural result that 0xFFFF has the value
65535 whether ints are 16 bits or longer.  If ints are 16 bits, the type
of 0xFFFF is unsigned int; if bits are longer, the type is int.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list