为什么不能从主函数返回更大的值?

时间:2022-02-09 20:58:23

I am trying to return a bigger value like 1000 from my main function, but when I type echo $? it displays 0.

我试图从主函数返回一个更大的值,比如1000,但是当我输入echo $时?它显示0。

If I return a smaller value like 100 it displays the correct value.

如果我返回一个更小的值,比如100,它会显示正确的值。

My Code:

我的代码:

int main(void)
{
     return 1000;
}

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

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

3 个解决方案

#1


22  

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.

这里有两个相关的概念:C退出状态和bash返回代码。它们都覆盖了0-255的范围,但是bash使用了超过126的数字,因为它是自己的目的,所以从您的程序中返回它们将会很混乱。

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.

为了安全,将退出状态代码限制为0-127,因为这是最可移植的,至少http://docs.python.org/library/sys.html#sys.exit是这么说的。

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.

C退出状态被放入bash $?但是bash使用127来指示“未找到命令”,因此您可能希望避免使用它。Bash参考页面。

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.

Bash还使用128-255作为信号——它们指示进程被一个信号杀死:退出代码= 128 +信号号。所以你可能会用接近255的数字侥幸逃脱,因为信号数字不太可能达到那么高。

Beyond those common guide-lines there are many attempts to define what different numbers should mean: http://tldp.org/LDP/abs/html/exitcodes.html.

除了这些常见的指导方针之外,还有许多尝试来定义不同的数字的含义:http://tldp.org/LDP/abs/html/exitcodes.html。

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.

如果你想从你的程序中返回一个任意的整数,最好将它打印到stdout中,然后从你的bash脚本中获取它的值=$(程序)。

#2


3  

The return value of main (i.e. the exit status of the application) is limited to the range [0, 255] on *NIX. 1000 is out of range, and the OS treats it as 0, presumably.

main的返回值(即应用程序的退出状态)仅限于*NIX上的[0,255]范围。1000是超出范围的,而OS将其视为0。

#3


1  

In Unix-land the return value of main is limited because exit there is limited to the range of an 8-bit byte.

在Unix-land中,main的返回值是有限的,因为出口限制在8位字节的范围之内。

In Windows there is a single value, STILL_ACTIVE with value 259, that is best avoided as process exit code.

在Windows中,有一个值STILL_ACTIVE,值为259,最好作为进程退出代码来避免。

Other than that, in Windows you can return a 32-bit code such as an HRESULT and that is commonly done.

除此之外,在Windows中,您可以返回一个32位的代码,比如HRESULT,这通常是这样做的。

#1


22  

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.

这里有两个相关的概念:C退出状态和bash返回代码。它们都覆盖了0-255的范围,但是bash使用了超过126的数字,因为它是自己的目的,所以从您的程序中返回它们将会很混乱。

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.

为了安全,将退出状态代码限制为0-127,因为这是最可移植的,至少http://docs.python.org/library/sys.html#sys.exit是这么说的。

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.

C退出状态被放入bash $?但是bash使用127来指示“未找到命令”,因此您可能希望避免使用它。Bash参考页面。

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.

Bash还使用128-255作为信号——它们指示进程被一个信号杀死:退出代码= 128 +信号号。所以你可能会用接近255的数字侥幸逃脱,因为信号数字不太可能达到那么高。

Beyond those common guide-lines there are many attempts to define what different numbers should mean: http://tldp.org/LDP/abs/html/exitcodes.html.

除了这些常见的指导方针之外,还有许多尝试来定义不同的数字的含义:http://tldp.org/LDP/abs/html/exitcodes.html。

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.

如果你想从你的程序中返回一个任意的整数,最好将它打印到stdout中,然后从你的bash脚本中获取它的值=$(程序)。

#2


3  

The return value of main (i.e. the exit status of the application) is limited to the range [0, 255] on *NIX. 1000 is out of range, and the OS treats it as 0, presumably.

main的返回值(即应用程序的退出状态)仅限于*NIX上的[0,255]范围。1000是超出范围的,而OS将其视为0。

#3


1  

In Unix-land the return value of main is limited because exit there is limited to the range of an 8-bit byte.

在Unix-land中,main的返回值是有限的,因为出口限制在8位字节的范围之内。

In Windows there is a single value, STILL_ACTIVE with value 259, that is best avoided as process exit code.

在Windows中,有一个值STILL_ACTIVE,值为259,最好作为进程退出代码来避免。

Other than that, in Windows you can return a 32-bit code such as an HRESULT and that is commonly done.

除此之外,在Windows中,您可以返回一个32位的代码,比如HRESULT,这通常是这样做的。