[comp.std.c] Greenhills "C" compiler

drt@cysog.UUCP (David R. Trinidad) (06/06/91)

1. I just received a reply from the Greenhills people on the
following code .. which compiles and runs correctly on AT&T,
mircosoft, borland and SCO:

#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");
}

2. 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.

3. AT&T .... promotes the first value to unsigned if the second
operator is unsigned therfore correctly comparing the if
statment.

4. The question is : What is the standard ? K+R, and ANSI
Direct answers to either this posting or preferably E-MAIL
below.

************************************************************************
Organization: 	Cycare Systems 
Internet : 	drt%cysog@hbiso.ma02.bull.com
UUNET:		uunet!hbiso!cysog!drt
Phone:		(602) 224 0555	| FAX: (602) 224 0872
System:		B.O.S. UNIX     |
	 UNIX isn't just an OS  | It's a way of life !!!!!
***********************************************************************

diamond@jit533.swstokyo.dec.com (Norman Diamond) (06/06/91)

In article <510@cysog.UUCP> drt@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@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.