C: exit的值
发布日期:2021-10-26 12:56:45 浏览次数:2 分类:技术文章

本文共 1445 字,大约阅读时间需要 4 分钟。

hot3.png

运行node的process.exit时候发现了以前忽视的一个问题:

$ node> process.exit(-1)$ echo $?255

我希望exit的值是-1,结果成了255。

===

给出的解释:

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).

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.

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

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

===

C语言示例:

示例1

#include 
int main() { return 0;}

退出码为0。

示例2

#include 
int main() { return -1;}

退出码为255.

示例3

#include 
#include
int main() { exit(-1);}

退出码为255.

转载于:https://my.oschina.net/letiantian/blog/656556

转载地址:https://blog.csdn.net/weixin_33782386/article/details/92473558 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:canvas 合并图片,贴二维码
下一篇:在pymongo中使用sort

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月21日 06时53分14秒