Name
atexit
Synopsis
Registers a function to be called when the program exits
#include <stdlib.h> intatexit( void (*func)( void ));
The argument of the atexit() function is a pointer to a
function of type void that has no
parameters. If the atexit() call
is successful, your program will call the function referenced by
this pointer if and when it exits normally. The atexit() call returns 0 to indicate that
the specified function has been registered successfully.
You may call atexit() up to
32 times in a program. If you register more than one function in
this way, they will be called in LIFO order: the last function
registered will be the first one called when your program
exists.
Example
int main()
{
void f1(void), f2(void);
printf("Registering the \"at-exit\" functions f1 and f2:");
if (atexit(f1) || atexit(f2) )
printf(" failed.\n");
else
printf(" done.\n");
printf("Exiting now.\n");
exit(0); // Equivalent to return 0;
}
void f1(void)
{ printf("Running the \"at-exit\" function f1().\n"); }
void f2(void)
{ printf("Running the \"at-exit\" function f2().\n"); }This code produces the following output:
Registering the "at-exit" functions f1 and f2: done. Exiting now. Running the "at-exit" function f2(). Running the "at-exit" function f1().
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access