Code:
short i, j;
i = -32768;
j = -i;
printf("%d", j);
trying to learn C. Well, a friend of mine (who is a programmer) asked me
(who is not a programmer) what the output of this would be, and why. I
had no clue, so I ran it, and it printed -32768. I gave him my best
guess (which was wrong):
1) a short maxes out at 32768, so it has no more room to hold the
sign info. That's it for i, anyway. When you define j as -i, it's
saying "here's some sign info, then look at i", not actually doing
the math as -1 * i.
sign info. That's it for i, anyway. When you define j as -i, it's
saying "here's some sign info, then look at i", not actually doing
the math as -1 * i.
2) There are little gremlins in my compiler changing signs of my
numbers around. Darn gremlins!
numbers around. Darn gremlins!
I have to say, I'm a little bothered by this. Why does it print -32768?
- Gio
--
AND NOW FOR A WORD (an IF blog):
Comment