C语言 返回 1、返回 0、返回 -1 和退出之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22604196/
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
Difference between return 1, return 0, return -1 and exit?
提问by Paolo
For example consider following code:
例如考虑以下代码:
int main(int argc,char *argv[])
{
int *p,*q;
p = (int *)malloc(sizeof(int)*10);
q = (int *)malloc(sizeof(int)*10);
if (p == 0)
{
printf("ERROR: Out of memory\n");
return 1;
}
if (q == 0)
{
printf("ERROR: Out of memory\n");
exit(0);
}
return 0;
}
What does return 0, return 1, exit(0)do in the above program?
exit(0)will exit total program and control comes out of loop but what happens in case of return 0, return 1, return -1.
是什么return 0,return 1,exit(0)在上面做计划?
exit(0)将退出整个程序并且控制退出循环,但是在return 0, return 1, 的情况下会发生什么return -1。
回答by Paolo
returnfrom main()is equivalent to exit
return从main()相当于exit
the program terminates immediately execution with exit statusset as the value passed to returnor exit
程序立即终止执行,退出状态设置为传递给return或的值exit
returnin an inner function (not main) will terminate immediately the execution of the specific function returning the given result to the calling function.
return在内部函数 (not main) 中,将立即终止特定函数的执行,将给定的结果返回给调用函数。
exitfrom anywhere on your code will terminate program execution immediately.
exit从您的代码的任何位置将立即终止程序执行。
status 0 means the program succeeded.
状态 0 表示程序成功。
status different from 0 means the program exited due to error or anomaly.
status 不为 0 表示程序由于错误或异常而退出。
If you exit with a status different from 0 you're supposed to print an error message to stderrso instead of using printfbetter something like
如果您以不同于 0 的状态退出,您应该打印一条错误消息,stderr而不是使用printf更好的类似
if(errorOccurred) {
fprintf(stderr, "meaningful message here\n");
return -1;
}
note that (depending on the OS you're on) there are some conventions about return codes.
请注意(取决于您使用的操作系统)有一些关于返回码的约定。
Google for "exit status codes" or similar and you'll find plenty of information on SO and elsewhere.
谷歌搜索“退出状态代码”或类似的,你会找到大量关于 SO 和其他地方的信息。
Worth mentioning that the OS itself may terminate your program with specific exit status codes if you attempt to do some invalid operations like reading memory you have no access to.
值得一提的是,如果您尝试执行一些无效操作(例如读取您无法访问的内存),操作系统本身可能会以特定的退出状态代码终止您的程序。
回答by giorashc
return nfrom your main entry function will terminate your process and report to the parent process (the one that executed your process) the result of your process. 0 means SUCCESS. Other codes usually indicates a failure and its meaning.
return n从您的主入口函数将终止您的流程并向父流程(执行您的流程的流程)报告您的流程结果。0 表示成功。其他代码通常表示故障及其含义。
回答by Sunil Yadav
To indicate execution status.
表示执行状态。
status 0 means the program succeeded.
状态 0 表示程序成功。
status different from 0 means the program exited due to error or anomaly.
status 不为 0 表示程序由于错误或异常而退出。
return n;from your main entry function will terminate your process and report to the parent process (the one that executed your process) the result of your process. 0 means SUCCESS. Other codes usually indicates a failure and its meaning.
返回 n; 从您的主入口函数将终止您的流程并向父流程(执行您的流程的流程)报告您的流程结果。0 表示成功。其他代码通常表示故障及其含义。
回答by Suvarna Pattayil
As explained here, in the context of mainboth returnand exitdo the same thing
正如这里所解释的,在main两者的上下文中return,exit做同样的事情
Q: Why do we need to returnor exit?
问:为什么我们需要return或exit?
A: To indicate execution status.
A:表示执行状态。
In your example even if you didnt have return or exit statements the code would run fine (Assuming everything else is syntactically,etc-ally correct. Also, if (and it should be) mainreturns intyou need that return 0at the end).
在您的示例中,即使您没有 return 或 exit 语句,代码也可以正常运行(假设其他所有内容在语法上等都是正确的。此外,如果(并且应该是)main返回int您最后需要它return 0)。
But, after execution you don't have a way to find out if your code worked as expected.
You can use the return code of the program (In *nix environments , using $?) which gives you the code (as set by exitor return) . Since you set these codes yourself you understand at which point the code reached before terminating.
但是,在执行之后,您无法确定您的代码是否按预期工作。您可以使用程序的返回代码(在 *nix 环境中,使用$?),它为您提供代码(由exit或设置return)。由于您自己设置了这些代码,因此您了解代码在终止之前到达的位置。
You can write return 123where 123indicates success in the post execution checks.
你可以写return 123在那里123表示,执行后检查的成功。
Usually, in *nix environments 0is taken as success and non-zero codes as failures.
通常,在 *nix 环境0中将成功视为成功,将非零代码视为失败。
回答by S?ren Debois
return nfrom mainis equivalent to exit(n).
return nfrommain等价于exit(n).
The valid returned is the rest of your program. It's meaning is OS dependent. On unix, 0 means normal termination and non-zero indicates that so form of error forced your program to terminate without fulfilling its intended purpose.
返回的有效内容是程序的其余部分。它的含义取决于操作系统。在 unix 上,0 表示正常终止,非零表示这种形式的错误迫使您的程序在没有达到预期目的的情况下终止。
It's unusual that your example returns 0 (normal termination) when it seems to have run out of memory.
您的示例在内存不足时返回 0(正常终止)是不寻常的。
回答by EmptyData
returnin function return execution back to caller and exitfrom function terminates the program.
returnin 函数返回执行返回调用者和exitfrom 函数终止程序。
in mainfunction return 0or exit(0)are same but if you write exit(0)in different function then you program will exit from that position.
在main函数中return 0或exit(0)相同但如果你exit(0)在不同的函数中编写,那么你的程序将从该位置退出。
returning different values like return 1or return -1means that program is returning error .
返回不同的值,例如return 1或return -1意味着程序返回 error 。
When exit(0)is used to exit from program, destructors for locally scoped non-static objects are not called. But destructors are called if return 0 is used.
当exit(0)用于退出程序时,不调用局部作用域非静态对象的析构函数。但是,如果使用 return 0,则会调用析构函数。

