What is difference between return and exit statement in C programming when called from anywhere in a C program?
return is a statement that returns the control of the flow of execution to the function which is calling. Exit statement terminates the program at the point it is used.
exit() is a system call which terminates current process. exit() is not an instruction of C language. Whereas, return() is a C language instruction/statement and it returns from the current function (i.e. provides exit status to calling function and provides control back to the calling function).
The exit() function has no return type. int status: It represents the status value of the exit function returned to the parent process.
A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.
for
or break
.The only case when both do (nearly) the same thing is in the main()
function, as a return from main performs an exit()
.
In most C implementations, main
is a real function called by some startup code that does something like int ret = main(argc, argv); exit(ret);
. The C standard guarantees that something equivalent to this happens if main
returns, however the implementation handles it.
Example with return
:
#include <stdio.h> void f(){ printf("Executing f\n"); return; } int main(){ f(); printf("Back from f\n"); }
If you execute this program it prints:
Executing f Back from f
Another example for exit()
:
#include <stdio.h> #include <stdlib.h> void f(){ printf("Executing f\n"); exit(0); } int main(){ f(); printf("Back from f\n"); }
If you execute this program it prints:
Executing f
You never get "Back from f". Also notice the #include <stdlib.h>
necessary to call the library function exit()
.
Also notice that the parameter of exit()
is an integer (it's the return status of the process that the launcher process can get; the conventional usage is 0 for success or any other value for an error).
The parameter of the return statement is whatever the return type of the function is. If the function returns void, you can omit the return at the end of the function.
Last point, exit()
come in two flavors _exit()
and exit()
. The difference between the forms is that exit()
(and return from main) calls functions registered using atexit()
or on_exit()
before really terminating the process while _exit()
(from #include <unistd.h>
, or its synonymous _Exit from #include <stdlib.h>
) terminates the process immediately.
Now there are also issues that are specific to C++.
C++ performs much more work than C when it is exiting from functions (return
-ing). Specifically it calls destructors of local objects going out of scope. In most cases programmers won't care much of the state of a program after the processus stopped, hence it wouldn't make much difference: allocated memory will be freed, file ressource closed and so on. But it may matter if your destructor performs IOs. For instance automatic C++ OStream
locally created won't be flushed on a call to exit and you may lose some unflushed data (on the other hand static OStream
will be flushed).
This won't happen if you are using the good old C FILE*
streams. These will be flushed on exit()
. Actually, the rule is the same that for registered exit functions, FILE*
will be flushed on all normal terminations, which includes exit()
, but not calls to _exit()
or abort().
You should also keep in mind that C++ provide a third way to get out of a function: throwing an exception. This way of going out of a function will call destructor. If it is not catched anywhere in the chain of callers, the exception can go up to the main() function and terminate the process.
Destructors of static C++ objects (globals) will be called if you call either return
from main()
or exit()
anywhere in your program. They wont be called if the program is terminated using _exit()
or abort()
. abort()
is mostly useful in debug mode with the purpose to immediately stop the program and get a stack trace (for post mortem analysis). It is usually hidden behind the assert()
macro only active in debug mode.
When is exit() useful ?
exit()
means you want to immediately stops the current process. It can be of some use for error management when we encounter some kind of irrecoverable issue that won't allow for your code to do anything useful anymore. It is often handy when the control flow is complicated and error codes has to be propagated all way up. But be aware that this is bad coding practice. Silently ending the process is in most case the worse behavior and actual error management should be preferred (or in C++ using exceptions).
Direct calls to exit()
are especially bad if done in libraries as it will doom the library user and it should be a library user's choice to implement some kind of error recovery or not. If you want an example of why calling exit()
from a library is bad, it leads for instance people to ask this question.
There is an undisputed legitimate use of exit()
as the way to end a child process started by fork() on Operating Systems supporting it. Going back to the code before fork() is usually a bad idea. This is the rationale explaining why functions of the exec() family will never return to the caller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With