int divided by unsigned.

Jim Giles jlg at lanl.gov
Fri Jun 16 09:47:31 AEST 1989


#include "stdio.h"
main(){
   int a;
   unsigned b;

   a= -5;
   b=1000;
   printf("%d\n",a/=b);
}

I tried the above program on my sun workstation and it printed 4294967.
This is aparently the "correct" answer according to the proposed C
standard.  On the Cray under UNICOS, the same program prints 0.  The
"correct" answer for the Cray would have been 18446744073709551.  This
is an example of a case where deviating from the C definition produces
desireable results.  I hope Cray doesn't "fix" their C compiler.

(Note: this behaviour occurs because C requires arguments to be "promoted"
to unsigned if either is already unsigned.  The preferable rule would be
that if one argument is an int and the other is an unsigned, _both_ should
be promoted to long before the operator is applied.  Unsigned is _not_ a
promotion from int - it is a break-even semantics change.)



More information about the Comp.lang.c mailing list