简体   繁体   English

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

[英]How can I get what my main function has returned?

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 1 or return 0 ;在每个main()我们写return 1return 0 how can I know what my main() has returned at terminal?我怎么知道我的main()在终端返回了什么?

Edit:1编辑:1

I get it that by echo $?我通过echo $?得到它echo $? we can get the return value of main() but it only allows me to return a value less then 125 (in Linux) successfully.我们可以获得main()的返回值,但它只允许我成功返回小于 125 的值(在 Linux 中)。 A return value more than that cannot be be successfully received by the $ variable so $ variable无法成功接收超过该值的返回值,因此

why is int the return type of main() ?为什么intmain()的返回类型? Why not keep it short int ?为什么不保持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,我从哪里可以找到错误代码的含义?

Most shells store the exit code of the previous run command in $?大多数 shell 将上一个运行命令的退出代码存储在$? so you can store or display it.这样您就可以存储或显示它。

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

Your shell probably has a special variable $?你的 shell 可能有一个特殊的变量$? , which holds the last program returned value. ,它保存最后一个程序返回的值。 So, soon after your program finishes, you can run:因此,在您的程序完成后不久,您就可以运行:

echo $?

to see the returned value.查看返回值。

In DOS/Windows you can use errorlevel within 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 42 really means "if errorlevel is 42 or greater"请记住从最大到最低检查,因为if errorlevel 42真的意味着“如果错误级别是 42 或更高”

Summarizing comments and bits and pieces so they're in one place.总结评论和点点滴滴,使它们集中在一个地方。

AC program always has an exit code , which the program may decide for itself if it terminates normally, by returning a value from the main function or by calling the exit function. AC 程序总是有一个退出代码,程序可以通过从main函数返回一个值或调用exit函数来自行决定它是否正常终止。 If the program terminates abnormally, for example by a segmentation fault, the operating system decides the exit code.如果程序异常终止,例如由于分段错误,操作系统将决定退出代码。

In Unix (Posix), the exit code is an 8-bit value: 0-255.在 Unix (Posix) 中,退出代码是一个 8 位值: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.有关详细信息,请参阅wait(2)手册页。

In Unix, at the shell, the status of the previous command is accessible as the $?在 Unix 中,在 shell 中,前一个命令的状态可以作为$? 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.因为退出代码只有 8 位,并且它被视为一个无符号整数,如果你返回一个负值,它就会变成一个正值:-1 变成 255。同样,如果你返回一个大于 255 的值,只有最少使用有效 8 位:256 变为 0。

The return type of main is int , rather than short or char , because there's no particular benefit in making it a smaller type, particularly at this point in history, decades after it was decided. main的返回类型是int ,而不是shortchar ,因为使它成为更小的类型并没有什么特别的好处,尤其是在历史上的这个时刻,也就是在它被决定几十年后。 Changing it now would only cause unnecessary complications.现在改变它只会导致不必要的并发症。

If you want to execute a program from C, the standard library provides the system function, which handily returns the status of the program.如果你想从 C 执行一个程序,标准库提供了system函数,它可以方便地返回程序的状态。 (Note that system runs 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.) (请注意, 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).为了获得更大的灵活性,您可以使用系统调用forkexecl (或其变体之一,请参阅exec(3)手册页)和wait (已在上面提到)来执行其他程序。 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.) (另一方面,学习这些东西非常有趣。)

You can get the exit values with the command basic linux command echo $?您可以使用命令 basic linux command echo $? 获取退出值。 The error codes are standard and the details are explained in this link错误代码是标准的,详细信息在此链接中说明

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-超出范围**

in windows command line to read return value from main use echo %errorlevel%在 Windows 命令行中从 main 中读取返回值使用echo %errorlevel%

(Code) return.c (代码) return.c

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
    int i;

    printf("Enter a number");
    scanf("%d",&i);

   if(i==2)
        exit(1);
   if(i==3)
        exit(2);
    
   return 0;
}

OUTPUT输出

OUTPUT of above program return.c上述程序的OUTPUT return.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何存储函数返回给我的main的指针(节点)? - How do I store a pointer (node) returned by a function to my main? 我在c中写了一个substr函数,但无法在main函数中获取返回值 - I wrote a substr function in c, but can not get the returned value in main function 如何从这个 function 取回我的主要 function 的迭代值? - How can I get back the value of iterations from this function to my main function? 我的 Main 函数只要求我输入 1 个。 如何获得将三个输入相乘的函数? - My Main function is only asking me for 1 input. How can I get the function to multiply three inputs? 我是否可以使用&运算符来获取函数中的有效指针,即使该函数已在golang中返回 - Can I use & operator to get a valid pointer in a function even if the function has returned in golang 我怎样才能只得到一个数字作为主要 function 的参数? - How can I get only one digit as an argument of the main function? 我无法将数组从函数中取出到 c 中的 main - I can´t get out my array from a function to the main in c 如何从一个 function 中的多个参数中获取值并将其打印在 c 的主 function 中? - How can I get value from multiple argument in one function and print it in main function in c? 如何删除主函数的头部? - How can I remove the head of a main function? 如何将字符串返回到主函数? - How can I return String to the main function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM