weird C behavior

Roy Mongiovi roy at gitpyr.UUCP
Sat Mar 22 18:17:31 AEST 1986


In article <557 at utastro.UUCP>, nather at utastro.UUCP (Ed Nather) writes:
> Equality NOT found: i = -28672, BIG = -28672

Ok.  The #defined constant is greater than 32767, so it's a long on a
16 bit machine.  Top 16 bits are zero, bottom are 0x9000.  It gets
assigned into an SIGNED integer, so that gets the bottom 16 bits.
Then to perform the comparison, C promotes the int to a long, and
since it's signed that extends the sign bit.  One value now has
the top 16 bits zero, the other has 0xFFFF.  Unfortunately, the
program ignores the fact that the constant is a long, and printf's
it through a %d.  Since words and bytes are stored in least significant,
most significant order in memory, the address passed to printf for both
variables is that of the bottom 16 bits, which are equal.  Therefore
the printed output seems to contradict the comparison.

The real question is:  who made the mistake?  The compiler by assuming
the constant is a long, or the programmer by printing with a %d?  Maybe
you ought to have to say L to get a long constant....
-- 
Roy J. Mongiovi.	Office of Computing Services.		User Services.
Georgia Institute of Technology.	Atlanta GA  30332.	(404) 894-6163
 ...!{akgua, allegra, amd, hplabs, ihnp4, masscomp, ut-ngp}!gatech!gitpyr!roy



More information about the Comp.lang.c mailing list