unsigned comparsions

Chris Torek chris at mimsy.UUCP
Mon May 2 08:56:04 AEST 1988


In article <375 at m3.mfci.UUCP> root at mfci.UUCP (SuperUser) writes:
>No, these two programs are not equivalent.

[They were equivalent.  The comparison was |unsigned char| != -1, which
---provided |char| is shorter than |int|---is indeed always true, in both
the existing sign preserving system and in the value preserving system in
the dpANS.]

>When comparing an unsigned [integer] to a signed integer, the signed
>integer is first cast to unsigned (which results in no change in the
>bit pattern), then the comparison is performed.

With the insertion I have made (which was implied, but given dpANS
rules one must be explicit), this is correct.

>since octal and hex constants are signed in C,

In K&R.  In dpANS C, some octal and hex constants are unsigned,
as are some integer constants.

>... People are often surprised by the fact that an expression like
>(u > -1) is always false when u is unsigned, since the -1 is first
>cast to unsigned, whereupon it becomes the largest possible unsigned.

Again, this is correct in both systems, with the proviso that |u| is
|unsigned int|, not |unsigned char| or |unsigned short|.  Under dpANS
rules, however, if |u| is |unsigned char| and |char| is shorter than
|int|, it will be always true; if |u| is |unsigned short| the test
will be either always true or always false, depending on the size
of |short| vs |int|.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list