A trick question
- From: Gio <giomancer@xxxxxxxxxxxxx>
- Date: Sat, 19 Apr 2008 22:03:55 GMT
[code]
short i, j;
i = -32768;
j = -i;
printf("%d", j);
[/code]
As some of you may have guessed by my previous post (about books), I'm
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.
My second guess was:
2) There are little gremlins in my compiler changing signs of my
numbers around. Darn gremlins!
Okay, so that one was wrong too. :(
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):
http://giomancer.wordpress.com/
.
- Follow-Ups:
- Re: A trick question
- From: Gio
- Re: A trick question
- From: CBFalconer
- Re: A trick question
- From: Joe Wright
- Re: A trick question
- From: Richard Heathfield
- Re: A trick question
- From: Ioannis Vranos
- Re: A trick question
- Prev by Date: Re: memset o/p not correct
- Next by Date: Re: A trick question
- Previous by thread: memset o/p not correct
- Next by thread: Re: A trick question
- Index(es):
Relevant Pages
|