Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C exit()的奇怪行为_C_Exit - Fatal编程技术网

C exit()的奇怪行为

C exit()的奇怪行为,c,exit,C,Exit,我试图用某个退出编号退出我的主函数,但它总是以0退出。例如,代码: printf("command exited with error: %i\n", commandExitError); if(commandExitError > 0) exit(commandExitError); if(openError > 0) exit(openError); printf("I shouldn't see this if there was an error"); return

我试图用某个退出编号退出我的主函数,但它总是以0退出。例如,代码:

printf("command exited with error: %i\n", commandExitError);
if(commandExitError > 0)
  exit(commandExitError);

if(openError > 0)
  exit(openError);
printf("I shouldn't see this if there was an error");
return 0;
有一种奇怪的行为,即如果
commandExitError
为0,但
openError
为1,那么它将以错误1退出。但是,如果
commandexitorror
大于零,它仍然以0退出!例如,这是带有
commandExitError>0
的一些输出:

命令已退出,错误为:512

请注意,我们从未到达print语句“我不应该看到这个…”,然后获取我的程序的退出状态echo$

0

我们看到我的程序仍然以0退出,尽管它显然应该以512退出。

根据

exit()函数导致正常进程终止,并将
status&0377
的值返回给父进程

八进制377是十进制255

512 & 255 = 0

exit()函数导致正常进程终止,并将
status&0377
的值返回给父进程

八进制377是十进制255

512 & 255 = 0

我不确定可能的退出代码的范围是否由任何标准定义,但GNUC库(作为示例)只接受0到255范围内的值

我不确定可能的退出代码的范围是否由任何标准定义,但GNU C库(作为示例)只接受0到255范围内的值

请复制确切的终端命令和输出。退出值为8位数量-
512%256==0
。尝试513、514、3、4、133、134等。请复制准确的终端命令和输出。退出值为8位数量-
512%256==0
。试试513、514、3、4、133、134等等。你以30秒的优势击败了我。(好的回答:)你以30秒的优势击败了我。(好的回答:)