C语言 “返回0”和“退出(0)”的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17383015/
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 0" and "exit (0)"
提问by haccks
Is there any difference between return 0and exit (0)when using in a function?
If yes, When should I use return 0or exit (0)in a function?
在函数中使用return 0和exit (0)使用时有什么区别吗?如果是,我应该什么时候使用return 0或exit (0)在函数中?
回答by ouah
returnexits from the function while exitexits from the program.
return退出exit程序时退出函数。
In mainfunction executing return 0;statement or calling exit(0)function will call the registered atexithandlers and will cause program termination.
在main函数中执行return 0;语句或调用exit(0)函数将调用注册的atexit处理程序并导致程序终止。
回答by 6502
exit 0is a syntax error in C. You can have exit(0)that is instead a call to a standard library function.
exit 0是 C 中的语法错误。您可以将exit(0)其改为调用标准库函数。
The function exitwill quit the whole program, returning the provided exit code to the OS. The returnstatement instead only quits the current function giving the caller the specified result.
该函数exit将退出整个程序,将提供的退出代码返回给操作系统。return相反,该语句仅退出当前函数,为调用者提供指定的结果。
They are the same only when used in main(because quitting the mainfunction will terminate the program).
它们仅在用于 in 时相同main(因为退出main函数将终止程序)。
Normally exitis only used in emergency cases where you want to terminate the program because there's no sensible way to continue execution. For example:
通常exit仅在您想终止程序的紧急情况下使用,因为没有继续执行的明智方法。例如:
//
// Ensure allocation of `size` bytes (will never return
// a NULL pointer to the caller).
//
// Too good to be true? Here's the catch: in case of memory
// exhaustion the function will not return **at all** :-)
//
void *safe_malloc(int size) {
void *p = malloc(size);
if (!p) {
fprintf(stderr, "Out of memory: quitting\n");
exit(1);
}
return p;
}
In this case if function acalls function bthat calls function cthat calls safe_mallocyou may want to quit the program on the spot instead of returning to can error code (e.g. a NULLpointer) if the code is not written to handle allocation failures.
在这种情况下,如果函数a调用b了调用函数的函数c,如果代码不是用来处理分配失败的,safe_malloc您可能希望当场退出程序而不是返回c错误代码(例如NULL指针)。
回答by Some programmer dude
Yes there is, since there is no statement called exit. I guess you mean the functionexit?
是的,因为没有称为exit. 我猜你是说函数exit?
In that case, there is a bigdifference: The exitfunction exits the process, in other words the program is terminated. The returnstatementsimply return from the current function.
在那种情况下,有一个很大的区别:exit函数退出进程,换句话说程序终止。该return语句只是从当前函数返回。
They are only similar if used in the mainfunction.
它们仅在main函数中使用时才相似。
回答by 0decimal0
returnis a statement that returns control back to the calling function.exitis a system call which terminates the current process i.e the currently executing program.
return是一个将控制权返回给调用函数的语句。exit是一个系统调用,它终止当前进程,即当前正在执行的程序。
In main()the return 0;and exit(0);perform the same thing.
在main()中return 0;和exit(0);执行同样的事情。
NOTE: you have to include #include<stdlib.h>.
注意:您必须包含#include<stdlib.h>.

