...
atexit()
is only called by exit()
or upon normal completion of main()
.
return
from main()
Since Because main()
is defined to have return type int
, another valid exit strategy is to simply use a return
statement.
...
Wiki Markup |
---|
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] (C99) sectionSection 5.1.2.2.3 has this to say about returning from {{main()}}: |
If the return type of the
main
function is a type compatible withint
, a return from the initial call to themain
function is equivalent to calling theexit
function with the value returned by themain
function as its argument; reaching the}
that terminates themain
function returns a value of 0. If the return type is not compatible withint
, the termination status returned to the host environment is unspecified.
So Consequently, returning from main()
is equivalent to calling exit()
. Many compilers implement this behavior with something analogous to:
...
Wiki Markup |
---|
However, making it out of main is conditional on correctly being able to handlehandling all errors in a way that does not force premature termination. (see \[[ERR00-A. Adopt and implement a consistent and comprehensive error handling policy]\] and \[[ERR05-A. Application-independent code must provide error detection without dictating error handling]\]). |
_Exit()
A more abrupt function, _Exit()
also takes one argument and never returns. The standard specifies that _Exit()
also closes open file descriptors, but does not specify if _Exit()
flushes file buffers or deletes temporary files. Functions registered by atexit()
are not executed.
...