Greenhills "C" compiler (casting int's)

Norman Diamond diamond at jit533.swstokyo.dec.com
Thu Jun 6 19:20:30 AEST 1991


In article <510 at cysog.UUCP> drt at cysog.UUCP (David R. Trinidad) writes:
>#include <stdio.h>
>#include <limits.h>
>int intermediate = 100;
>main() {
>	if (intermediate > USI_MAX -1 ) printf("this is a bug");
>	if ((unsigned)intermediate > USI_MAX -1 ) printf("this works");
>}
>The response from GREENHILLS:
>If a operand is unsined int , the second is converted in unsigned int (sic).
>If an operand is int the second is converted in int (signed) [(sic)]
>To get a correct value you must cast the first value to unsigned.
>The question is : What is the standard ? K+R, and ANSI

If that response is quoted correctly, it is completely out to lunch.
In C (both K&R-1, including older implementations, and ANSI, including
K&R-2), the datatype used depends on both operands, not just on the first
operand.  And either or both of the operands are converted, if necessary,
to the datatype used for the operation.

In K&R-1, given an operation on a signed object and an unsigned one, the
signed one would always be converted to unsigned and the comparison would
be unsigned.

In ANSI, the rules are more complicated.  However, by sections 3.3.8 and
3.2.1.5, for an int and an unsigned int, the result will be the same --
the int is converted to unsigned int and the comparison is unsigned.

Incidentally, I'm not quite sure how your program obtained a definition
of USI_MAX.  It's not defined in ANSI C, so your program should have to do
something (such as #define POSIX_SOURCE) in order to get it, but I have the
vague impression that POSIX doesn't define this either.  (This impression
comes from trying to make sense of /usr/include/limits.h on my machine....)
--
Norman Diamond       diamond at tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
Permission is granted to feel this signature, but not to look at it.



More information about the Comp.std.c mailing list