On Wed, 1 Sep 2004, Mark Whitis wrote:
> I was trying to debug my printf routine and see why it was
> printing the wrong value. It wasn't. The problem was
> actually in the compiler output The compiler initializes
> the long constant -65535 as "1".
Actually, it looks like a similar problem to what someone else
had recently. The compiler was doing 16 bit math on an initializer
for a 32 bit number, even though the number was outside the range
for a signed int and should have been assumed to be a long based
on its magnitude if not its context (C isn't very smart about context)
or number of digits in hexadecimal representation.
Adding a "L" to the end of the number fixed the problem. Plain gcc
doesn't have this problem because an int is 32 bits. If it treated
0x0000FFFF as (signed int) -1, then it would also have treated "32768" as
(signed int) -32768 which when multipled by -1 should have yeilded +32768
which in signed int is really -32768 which was actually the desired
Section 2.3 of K&R second edition says:
"an integer too big to fit into an int will also be taken as a long"
It isn't specific about exactly what happens in the grey area between
32768 and 65535
Mark Whitis http://www.freelabs.com/~whitis/
Author of many open source software packages.
Coauthor: Linux Programming Unleashed (1st Edition)