Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do return codes over 255 return a differnt number in C++?

For example...

#include <iostream>

using namespace std;

int main(){return 300;}

Returns:

Process finished with exit code 44

??

like image 598
leszakk Avatar asked Jan 20 '16 23:01

leszakk


1 Answers

The standard knows only two standaradized return values: EXIT_SUCCESS (or zero) and EXIT_- FAILURE:

3.6.1/5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument.

18.5/8 (...) Finally, control is returned to the host environment. If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_- FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

It is hence not guaranteed that any other integer is returned as is.

On MS Windows for example, the GetExitCodeProcess() function returns the integer value so you'll get 300.

On POSIX compliant systems, like Linux, the rule is that ("only the 8 least significant bits (i.e. status & 0377) shall be available to the awaiting parent process"). So for 300, it will be 44.

like image 94
Christophe Avatar answered Nov 15 '22 05:11

Christophe