Your program can arrange to run its own cleanup functions if normal
termination happens. If you are writing a library for use in various
application programs, then it is unreliable to insist that all
applications call the library's cleanup functions explicitly before
exiting. It is much more robust to make the cleanup invisible to the
application, by setting up a cleanup function in the library itself
using atexit or on_exit.
— Function: int atexit (void (*function) (void))
The atexit function registers the function function to be
called at normal program termination. The function is called with
The return value from atexit is zero on success and nonzero if
the function cannot be registered.
This function is a somewhat more powerful variant of atexit. It
accepts two arguments, a function function and an arbitrary
pointer arg. At normal program termination, the function is
called with two arguments: the status value passed to exit,
and the arg.
This function is included in the GNU C library only for compatibility
for SunOS, and may not be supported by other implementations.
Here's a trivial program that illustrates the use of exit and