为什么不能从主函数返回更大的值? [英] Why cant' I return bigger values from main function?

查看:134
本文介绍了为什么不能从主函数返回更大的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图返回一个更大的值,如1000形式我的主要功能,但当我输入 echo $?它显示0。

I am trying to return a bigger value like 1000 form 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.

我的代码:

My Code :

int main(void)
{
     return 1000;
}

我们可以返回的值有什么限制吗?

Is there any limitation on the values which we can return ?

感谢。

推荐答案

这里有两个相关的概念:C exit status, 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的信号 - 它们表示进程被一个信号杀死:
退出代码= 128 +信号号。因此,你可能可以使用接近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,并用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆