Reputation: 19315
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!
Upvotes: 0
Views: 351
Reputation: 1296
-6 is represented as 2's compliment of 6 in binary in order to store it into memory.
Upvotes: 0
Reputation: 16865
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_function_%28programming%29
Upvotes: 0
Reputation: 15679
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.
Upvotes: 0
Reputation: 169763
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 [...]
Upvotes: 0
Reputation: 76601
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.
Upvotes: 2
Reputation:
I assume xcode treat the exit status as 00-FF (0-255) and -6 = 250 in that case
Upvotes: 0
Reputation: 411192
Return codes are interpreted as unsigned integers with the range 0-255 by the shell.
Upvotes: 9