is this broken or what?

MARK LEVY mark.levy at canremote.uucp
Sun Feb 4 21:23:00 AEST 1990


dP>Are we to infer from this discussion that the ANSI standard requires
dP>comparisons like the one we are talking about to work properly?  That
dP>is, what is the prescribed behavior of

dP>        int i=MAXINT, j=(-MAXINT);
dP>        if (i > j) printf ("foo");

dP>Since i-j is negative, not positive, I would expect most compilers
dP>*not* to print "foo".
   
   I am not up on the ANSI standards that have been proposed, but as
I recall, the K&R standard was 0 == FALSE and anything else == TRUE.

dP>I don't even know what happens when you compare an unsigned value to a
dP>signed value.  That is, in the test

dP>        unsigned int i;
dP>        if (i >= 0) ...

   The use of 2's complement will avoid any kind of problem of 
positive or negative zero.  A compiler might flag an incompatible
data type comparison here.  My guess is that if the high order bit
in i is set, the expression will evaluate false, provided that it
compiled.

dP>is i converted to a signed type before the comparison is performed?
dP>This could certainly make a mess if i is all ones.

   Don't ever assume anything about type conversions.  If you're not
sure, use pleanty of casts and parens.

Mark
---
 ~ DeLuxe 1.11a18 #3019
 ~ QNet 2.04a:NorthAmeriNet: Sound Advice BBS ~ Gladstone ~ MO



More information about the Comp.lang.c mailing list