Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!utfyzx!sq!msb From: msb@sq.UUCP Newsgroups: comp.lang.c Subject: Re: unsigned short vs. negative constant Message-ID: <1987May8.172821.4163@sq.uucp> Date: Fri, 8-May-87 17:28:21 EDT Article-I.D.: sq.1987May8.172821.4163 Posted: Fri May 8 17:28:21 1987 Date-Received: Sun, 10-May-87 02:03:36 EDT References: <7404@elsie.UUCP> Reply-To: msb@sq.UUCP (Mark Brader) Organization: SoftQuad Inc., Toronto Lines: 38 Checksum: 41829 Arthur David Olson (ado@elsie.UUCP) writes: > Thanks to everyone who replied to replied to my question about what the code > #include "stdio.h" > main() > { > unsigned short us; > us = ~0; > (void) printf("%d\n", us == -1); > } > should print on systems where sizeof short < sizeof int, But he didn't include some points from my reply, which I think are worth posting, so I am disregarding his Followup-to and posting this. (Arthur: did you get my mail at all?) The first thing is that there is no such thing as a "negative constant" in C. Both "~0" and "-1" are int constant-EXPRESSIONS. They have the same value on 2's complement machines, but will differ on most or all other types of machines. (YES, there are non-2's-complement machines running C.) This, then, is another nonportability in the above code... and remains one under the Draft ANSI Standard, unlike the unsigned short / int interaction. The second thing is that although it's true that... > * K&R can't be used to resolve the issue, since it predates unsigned > shorts. ...nevertheless one may make an inference by considering the interaction of longs with unsigned ints, which IS specified in K&R. In THAT case the corresponding code must print 0. And a final note. I hate it when people just post and say "this is the way it works on MY machine", but since I was posting anyway, this is the way it works on my machine. sq is a Sun running SunOS 3.0, and it prints 1 without a warning. I guess we don't have the "latest" system, since Arthur said... > * The latest Sun systems print 1; issuing a warning during compilation. Mark Brader, Toronto { decvax | ihnp4* | watmath | ... } !utzoo!sq!msb or via { hplabs | lll-crg | ... } !seismo!mnetor!utzoo!sq!msb *Avoid -- overloaded. or via decwrl!utcsri!sq!msb