tags:

views:

197

answers:

7

Hi, I'm just learning C and am using xCode for it (not sure if it matters). This code:

#include <stdio.h>

int main (int argc, const char * argv[]) { 
    int myInt;
    myInt = 2; 
    myInt *= ( (3*4) / 2 ) - 9; 
    printf("myInt = %d", myInt);
    return myInt; 
}

Outputs this:

Session started at 2009-11-09 15:51:15 -0500.]
myInt = -6
The Debugger has exited with status 250.The Debugger has exited with status 250.

The printf is right, but what about return is

A) making it wrap under, and

B) show the results twice? (to clarify, if I put return 0;, it only prints the "debugger has exited" line once, with the value as 0.)

Thanks!

+10  A: 

Return codes are interpreted as unsigned integers with the range 0-255 by the shell.

mipadi
Are you sure it's return codes are to the byte? I think I remember somehow getting a -1 return value and I remember it being more behaved as an unsigned integer,(as in, 32 or 64 bits depending on platform)... I think this is platform dependent however on the absolute upper and lower limits of return values
Earlz
On Mac OS X (which is what the question applies to, since it's tagged with `xcode`) the return value is an unsigned byte. It's been that way on all the Unix and Linux systems I've used, too, although I suppose it could technically be platform-dependent.
mipadi
I tested it on my OpenBSD system and it does just byte return values.. maybe it's Windows then that allows negatives and such
Earlz
A: 

I assume xcode treat the exit status as 00-FF (0-255) and -6 = 250 in that case

RC
+2  A: 

In Unix, the return value from a program is limited to the range 0-255 (yes, the return type from main is int, but that's a historical anomaly). Check out this GNU documentation on exit status.

As for the message "The Debugger has exited with status 250" being displayed twice, that is coming from the process that is running your command and so you have no control over it.

R Samuel Klatchko
A: 

Check the man page on exit() and _Exit():

Both functions make the low-order eight bits of the status argument available to a parent process [...]

Christoph
A: 

The wrap under (as you describe it) is a result of failing to output a LF (line feed) character as part of your call to printf(). You can fix that by adding \n to the print format string. Change your code to this:

#include <stdio.h>

int main (int argc, const char * argv[]) { 
    int myInt;
    myInt = 2; 
    myInt *= ( (3*4) / 2 ) - 9; 
    printf("myInt = %d\n", myInt);
    return myInt; 
}

As for doubling 'The Debugger has exited with status 250.' that's a function of your IDE / debugger, and not the result of your code. As others explained, -6 = 0xFFFFFFFA, which when truncated to 8 bits and treated as unsigned, equals 250 in decimal.

Craig Trader
A: 

Your return code for main should be 0 unless in error. This is a common convention in unix and doesn't hurt anything in windows. http://en.wikipedia.org/wiki/Main%5Ffunction%5F%28programming%29

xenoterracide
A: 

-6 is represented as 2's compliment of 6 in binary in order to store it into memory.

wrapperm