为什么我不能从主函数返回更大的值? [英] Why can't I return bigger values from main function?
问题描述
我试图从我的主函数返回一个更大的值,例如1000,但是当我键入echo $?
时它显示0.
I am trying to return a bigger value like 1000 from my main function, but when I type echo $?
it displays 0.
如果我返回一个较小的值(如100),则会显示正确的值.
If I return a smaller value like 100 it displays the correct value.
我的代码:
int main(void)
{
return 1000;
}
我们可以返回的值是否有限制?
Is there any limitation on the values which we can return?
推荐答案
此处有两个相关概念:C退出状态和bash返回代码.它们都覆盖0-255的范围,但是bash出于自身目的使用大于126的数字,因此从程序中返回这些数字会造成混乱.
There are two related concepts here: C exit status, and bash return code. They both cover the range 0-255, but bash uses numbers above 126 for it's own purposes, so it would be confusing to return those from your program.
为安全起见,将退出状态代码限制为0-127,因为这是最可移植的,至少 http://docs.python.org/library/sys.html#sys.exit .
To be safe limit exit status codes to 0-127, as that is most portable, at least that is implied by http://docs.python.org/library/sys.html#sys.exit.
将C退出状态放入bash $?变量执行后,但是bash使用127表示未找到命令",因此您可能要避免这种情况. Bash参考页.
The C exit status is put into the bash $? variable after execution, but bash uses 127 to indicate 'command not found' so you may want to avoid that. Bash reference page.
Bash还使用128-255作为信号-它们指示进程已被信号杀死:
exit code = 128 + signal number
.因此,您可以使用接近255的数字来摆脱困境,因为信号数字不太可能会升至如此之高.
Bash also uses 128-255 for signals - they indicate the process was killed with a signal:
exit code = 128 + signal number
. So you might be able to get away with using numbers close to 255 as it unlikely that signal numbers will go that high.
除了这些通用准则外,还进行了许多尝试来定义不同数字的含义: http://tldp.org/LDP/abs/html/exitcodes.html .
Beyond those common guide-lines there are many attempts to define what different numbers should mean: http://tldp.org/LDP/abs/html/exitcodes.html.
因此,您想从程序中返回任意整数,最好将其打印到stdout,并使用bash脚本中的VALUE=$(program)
捕获它.
So it you want to return an arbitrary integer from your program, it's probably best to print it to stdout, and capture it with VALUE=$(program)
from your bash script.
这篇关于为什么我不能从主函数返回更大的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!