为什么 return 代码超过 255 return 在 C++ 中是不同的数字?

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

例如...

#include <iostream>

using namespace std;

int main(){return 300;}

Returns:

Process finished with exit code 44

??

该标准只知道两个标准化的 return 值:EXIT_SUCCESS(或零)和 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.

因此不能保证任何其他整数按原样 returned。

MS Windows for example, the GetExitCodeProcess() 函数中 return 是整数值,因此您将得到 300。

关于POSIX compliant systems, like Linux,规则是("only the 8 least significant bits (i.e. status & 0377) shall be available to the awaiting parent process")。所以对于 300,它将是 44。