Linux 我怎样才能得到我的主函数返回的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8626109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:48:21  来源:igfitidea点击:

How can I get what my main function has returned?

clinuxgcc

提问by Jeegar Patel

In a C program if we want to give some input from terminal then we can give it by:

在 C 程序中,如果我们想从终端提供一些输入,那么我们可以通过以下方式提供:

int main(int argc, char *argv[])

In the same way, if we want to get return value of main()function then how can we get it?

同理,如果我们想获取main()函数的返回值,那如何获取呢?

In each main()we write return 1or return 0; how can I know what my main()has returned at terminal?

在每个main()我们写return 1return 0; 我怎么知道我main()在终端返回了什么?

Edit:1

编辑:1

I get it that by echo $?we can get the return value of main()but it only allows me to return a value less then 125 (in Linux) successfully. A return value more than that cannot be be successfully received by the $ variableso

我知道echo $?我们可以得到 的返回值,main()但它只允许我成功返回小于 125(在 Linux 中)的值。超过这个值的返回值不能被$ variableso成功接收

why is intthe return type of main()? Why not keep it short int?

为什么int返回类型是main()?为什么不保留它short int

Edit2

编辑2

From where can I find out the meaning of the error code if main()returns a value greater than 125?

如果main()返回的值大于 125 ,我从哪里可以找到错误代码的含义?

采纳答案by CB Bailey

Most shells store the exit code of the previous run command in $?so you can store or display it.

大多数 shell 将上一个运行命令的退出代码存储在其中,$?以便您可以存储或显示它。

$ ./a.out
$ echo $?     # note - after this command $? contains the exit code of echo!

or

或者

$ ./a.out
$ exit_code=$?    # save the exit code in another shell variable.

Note that under linux, although you return an int, generally only values less than 126 are safe to use. Higher values are reserved to record other errors that might occur when attempting to run a command or to record which signal, if any, terminated your program.

请注意,在 linux 下,虽然您返回的是int,但通常只有小于 126 的值才能安全使用。保留较高的值以记录尝试运行命令时可能发生的其他错误或记录哪个信号(如果有)终止了您的程序。

回答by sidyll

Your shell probably has a special variable $?, which holds the last program returned value. So, soon after your program finishes, you can run:

你的 shell 可能有一个特殊的变量$?,它保存最后一个程序返回的值。因此,在您的程序完成后不久,您就可以运行:

echo $?

to see the returned value.

查看返回值。

回答by pmg

In DOS/Windows you can use errorlevelwithin a batch file

在 DOS/Windows 中,您可以errorlevel在批处理文件中使用

executable optional arguments
if errorlevel 4 goto LABEL4
if errorlevel 3 goto LABEL3
if errorlevel 2 goto LABEL2
if errorlevel 1 goto LABEL1
:SUCCESS
echo SUCCESS; errorlevel 0
goto :eof
:LABEL1
echo FAILURE; errorlevel 1
goto :eof
:LABEL2
echo FAILURE; errorlevel 2
goto :eof
REM ...

Just remember to check from the greatest to the lowest because if errorlevel 42really means "if errorlevel is 42 or greater"

请记住从最大到最低检查,因为if errorlevel 42真正的意思是“如果错误级别为 42 或更高”

回答by pmg

Summarizing comments and bits and pieces so they're in one place.

总结评论和点点滴滴,使它们集中在一个地方。

A C program always has an exit code, which the program may decide for itself if it terminates normally, by returning a value from the mainfunction or by calling the exitfunction. If the program terminates abnormally, for example by a segmentation fault, the operating system decides the exit code.

AC 程序总是有一个退出代码,程序可以通过从main函数返回值或调用exit函数来自行决定它是否正常终止。如果程序异常终止,例如由于分段错误,操作系统将决定退出代码。

In Unix (Posix), the exit code is an 8-bit value: 0-255. It is combined with some other metadata to a status: the other metadata includes information about whether the program terminated normally or not, if it was terminated because of a signal, and if so, which signal. For details, see the wait(2)manual page.

在 Unix (Posix) 中,退出代码是一个 8 位值:0-255。它与一些其他元数据组合成一个状态:其他元数据包括有关程序是否正常终止、是否由于信号而终止以及如果是,是哪个信号的信息。有关详细信息,请参阅wait(2)手册页。

In Unix, at the shell, the status of the previous command is accessible as the $?special variable. Because the exit code is only 8 bits, and it's treated as an unsigned integer, if you return a negative value, it gets turned into a positive one: -1 becomes 255. Likewise, if you return a value greater than 255 only the least significant 8 bits are used: 256 becomes 0.

在 Unix 中,在 shell 中,前一个命令的状态可以作为$?特殊变量访问。因为退出代码只有 8 位,并且它被视为一个无符号整数,如果你返回一个负值,它就会变成一个正值:-1 变成 255。同样,如果你返回一个大于 255 的值,只有最少使用有效 8 位:256 变为 0。

The return type of mainis int, rather than shortor char, because there's no particular benefit in making it a smaller type, particularly at this point in history, decades after it was decided. Changing it now would only cause unnecessary complications.

的返回类型mainint,而不是shortor char,因为使它成为更小的类型并没有什么特别的好处,特别是在历史上的这个时候,在它被决定几十年后。现在改变它只会导致不必要的并发症。

If you want to execute a program from C, the standard library provides the systemfunction, which handily returns the status of the program. (Note that systemruns commands via the shell, and you need to be very careful about escaping everything correctly if you give the command any externally provided filenames or other things on the command line.)

如果你想从 C 执行一个程序,标准库提供了这个system函数,它可以方便地返回程序的状态。(请注意,system通过 shell 运行命令,如果在命令行上为命令提供任何外部提供的文件名或其他内容,则需要非常小心地正确转义所有内容。)

For more flexibility, you can execute other programs using the system calls fork, execl(or one of its variants, see the exec(3)manual page), and wait(already mentioned above). This is powerful and flexible, but it's also easy to make mistakes, so be sure to read the documentation and check out some example programs first. (On the other hand, it's very much fun to learn this stuff.)

为了获得更大的灵活性,您可以使用系统调用forkexecl(或其变体之一,请参阅exec(3)手册页)和wait(已经在上面提到)来执行其他程序。这功能强大且灵活,但也很容易出错,因此请务必先阅读文档并查看一些示例程序。(另一方面,学习这些东西非常有趣。)

回答by Shameerariff

You can get the exit values with the command basic linux command echo $? The error codes are standard and the details are explained in this link

您可以使用命令 basic linux command echo $? 获取退出值。错误代码是标准的,详细信息在此链接中说明

The general codes are

一般代码是

**

**

0- success

0-成功

1- general errors

1- 一般错误

126- permission issue

126-权限问题

127-Illegal command

127-非法命令

128-Invalid arguments and fatal errors

128-无效参数和致命错误

255-Out of range**

255-超出范围**