return statement vs exit() in main()
Actually, there is a difference, but it's subtle. It has more implications for C++, but the differences are important.
When I call
main(), destructors will be called for my locally scoped objects. If I call
exit(), no destructor will be called for my locally scoped objects! Re-read that.
exit() does not return. That means that once I call it, there are "no backsies." Any objects that you've created in that function will not be destroyed. Often this has no implications, but sometimes it does, like closing files (surely you want all your data flushed to disk?).
static objects will be cleaned up even if you call
exit(). Finally note, that if you use
abort(), no objects will be destroyed. That is, no global objects, no static objects and no local objects will have their destructors called.
Proceed with caution when favoring exit over return.
When to use exit() over return?
These two are very different in nature.
exit()is used when you want to terminate program immediately. If a call to
exit()is encountered from any part of the application, the application finishes execution.
returnis used to return the program execution control to the caller function. In case of
returnfinishes the execution.
To clarify about the case when used in
main(), quoting directly from the
C11 standard, chapter §220.127.116.11.3, Program termination,
returntype of the
main()function is a type compatible with
returnfrom the initial call to the
main()function is equivalent to calling the
exit()function with the value returned by the
main()function as its argument;11) reaching the
}that terminates the
main()function returns a value of
0. If the return type is not compatible with
int, the termination status returned to the host environment is unspecified.
So, basically, either
will behave as same in the context of
Difference between return 0 and exit (0)
return exits from the function while
exit exits from the program.
main function executing
return 0; statement or calling
exit(0) function will call the registered
atexit handlers and will cause program termination.
does return 0 exit a program or exit a loop?
It will return from the main method, which in this case, will exit the whole program.
Java - Could a return statement be used to exit a program?
return from the
main() method will exit the whole program.
Caveat: If you start (non-daemon) threads, the JVM will wait for those threads to finish before shutting down.
Difference between return and exit in Bash functions
man bash on
Causes a function to stop executing and return the value specified by n to its caller. If n is omitted, the return status is that of the last command executed in the function body.
Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed. A trap on EXIT is executed before the shell terminates.
As per your edit of the question, regarding exit codes,
return has nothing to do with exit codes. Exit codes are intended for applications/scripts, not functions. So in this regard, the only keyword that sets the exit code of the script (the one that can be caught by the calling program using the
$? shell variable) is
My last statement referring
exit is causing some comments. It was made to differentiate
exit for the understanding of the OP, and in fact, at any given point of a program/shell script,
exit is the only way of ending the script with an exit code to the calling process.
Every command executed in the shell produces a local "exit code": it sets the
$? variable to that code, and can be used with
&& and other operators to conditionally execute other commands.
These exit codes (and the value of the
$? variable) are reset by each command execution.
Incidentally, the exit code of the last command executed by the script is used as the exit code of the script itself as seen by the calling process.
Finally, functions, when called, act as shell commands with respect to exit codes. The exit code of the function (within the function) is set by using
return. So when in a function
return 0 is run, the function execution terminates, giving an exit code of 0.
How to Output Coloured Text to a Linux Terminal
When Can Outer Braces Be Omitted in an Initializer List
Is There a C Pre-Processor Which Eliminates #Ifdef Blocks Based on Values Defined/Undefined
Why Do Constant Expressions Have an Exclusion For Undefined Behavior
How to Construct a Std::String With Embedded Values, I.E. "String Interpolation"
Type of Integer Literals Not Int by Default
What's a Proper Way of Type-Punning a Float to an Int and Vice-Versa
Getting Size of Array from Pointer C++
No Matching Function - Ifstream Open()
Convert String to Variable Name or Variable Type
What Open Source C++ Static Analysis Tools Are Available
Writing Your Own Stl Container
Error: Passing Xxx as 'This' Argument of Xxx Discards Qualifiers
Why Are Unnamed Namespaces Used and What Are Their Benefits
Understanding the Meaning of the Term and the Concept - Raii (Resource Acquisition Is Initialization)