weird C behavior

Bill Crews bc at cyb-eng.UUCP
Thu Apr 3 10:30:40 AEST 1986


> /* weird.c - demonstrate weird C behavior */
> /* Reference: Kernighan and Ritchie pp. 40-41 */
. . .
> #define BIG 36864
> 
> main()
> {
> int i;
> 
> i = BIG;
> if(i == BIG)
>     printf("Equality found: i = %d, BIG = %d\n", i, BIG);
> else
>     printf("Equality NOT found: i = %d, BIG = %d\n", i, BIG);
> }
> 
> Equality NOT found: i = -28672, BIG = -28672
> 
> -----------------------------------------------------------------------
> Ed Nather
> Astronomy Dept, U of Texas @ Austin

It seems to me that your compiler has done the right thing.  Since 36864
cannot be represented as an int on your machine, it generated a positive
long rather than a negative int.  When i is extended to a long, the sign
bit is extended, so the comparison fails, as it should.  The only reason
you got -28672 for BIG instead of nulls is because your machine has backwards
byte order.
-- 
	- bc -

..!{seismo,topaz,gatech,nbires,ihnp4}!ut-sally!cyb-eng!bc  (512) 835-2266



More information about the Comp.lang.c mailing list