A trick question



[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/
.



Relevant Pages

  • Re: Delphi makes it to digg!
    ... so many times from Steve McConnell's books. ... I see today the programmer rather like a doctor, curator, social assistant in helping the user in his pains rather than a mathematician in his cubicle trying to implement a certain algorithm dictated by theory. ... About 'waterfall thinking' -> 'take a few steps and revisit': ... same mistakes, though you clearly identify them and as a team declare ...
    (borland.public.delphi.non-technical)
  • Re: OT: Programming in C
    ... I am looking for a couple of good books on learning C. ... at the advanced programmer with that one. ... have to design the data structures, and C++ is object oriented, so while ... car might be car.turnin object code. ...
    (Fedora)
  • Re: Good reference manual?
    ... learned that it's based on the WAMP stack. ... simpler for the programmer), so I'm not claiming to be brilliant. ... and I really don't have the money to get separate advanced books on ... Moo moo moo ...
    (comp.lang.php)
  • AT&T or Intel syntax ?
    ... I started looking into a book called Linux Assembly Language ... The books says some good points about Nasm and its ... syntax matching that of Intel's.It considered AT&T syntax obscure and ... Do you people have any advice to someone like me who is a C programmer ...
    (comp.lang.asm.x86)
  • Re: Which to use VB.NET or C#?
    ... hopelessly "dependent" on VStudio for its integrated IDE, ... its terribly useful and programmer efficiency enhancing ... Author of .NET books for O'Reilly Media ...
    (microsoft.public.dotnet.framework.aspnet)