C: exit的值

運行node的process.exit時候發現了之前忽視的一個問題:node

$ node
> process.exit(-1)

$ echo $?
255

我但願exit的值是-1,結果成了255。windows

===this

http://stackoverflow.com/questions/12512177/exit-status-of-a-program 給出的解釋:code

Program return values mean different things on different operating systems. On Unix-y systems, only values 0-255 are valid (1 unsigned byte). Note this means negative values are not allowed, and may be the cause of your IDE problems. FreeBSD introduced a load of specific meanings in sysexits.h, and while this certainly exists on Linux distributions I've used, the only real universally accepted one is 0 for success and non-0 for failure. As Pete Becker points out, EXIT_SUCCESS and EXIT_FAILURE are standard defined macros (0 and 1 respectively on Unix-y platforms, but implementation defined).orm

On windows, you get a 32 bit signed integer (negatives are allowed, don't know if they mean anything). Again, 0 is success, and other values mean various things. Like echo $?, you can use echo %errorlevel% to see the last program's exit code in the console.ci

Also note that my N3337 (draft very close to C++11 standard) says at section 3.6.1 paragraph 5:get

If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;it

===io

C語言示例:console

示例1

#include <stdio.h>

int 
main() 
{

    return 0;

}

退出碼爲0。

示例2

#include <stdio.h>

int 
main() 
{

    return -1;

}

退出碼爲255.

示例3

#include <stdio.h>
#include <stdlib.h>

int 
main() 
{

    exit(-1);

}

退出碼爲255.

相關文章
相關標籤/搜索