Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the program return with an exit code other than I specified?

Tags:

c

exit-code

This is a simple program :

int main() {
    return 0;
}

The exit code is 0.

If I write:

int main() {
    return 700;
}

The exit code is 188.

Why is 188 instead of 700 the exit code here?

like image 917
Reezoo Avatar asked Dec 05 '15 21:12

Reezoo


1 Answers

While the main function in C returns an int, operating systems don't necessarily use int as the error code.

700 in binary is 1010111100.
Truncating this value to eight bits yields 10111100.
This equals 188 in decimal.

That means your OS uses eight bits for error codes.1


1 Or possibly nine bits because the 8th bit (we start counting from 0, mind you) is 0 here. This is highly improbably due to 9 not being a power of 2, though, as is convention for data widths.

like image 126
cadaniluk Avatar answered Oct 06 '22 01:10

cadaniluk