atexit(S) 6 January 1993 atexit(S) Name atexit - register function to be called at termination Syntax cc . . . -lc #include <stdlib.h> int atexit(func) void (*func)(void); Description The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed ``last in, first out.'' No more than 32 functions can be registered with atexit. The functions passed to atexit cannot take parameters. Return value atexit returns zero on success or -1 on failure. See also abort(S), exit(S) Example #include <stdlib.h> #include <stdio.h> main( ) { int fn1( ), fn2( ), fn3( ), fn4( ); atexit(fn1); atexit(fn2); atexit(fn3); atexit(fn4); printf("This is executed first.\n"); } int fn1( ) { printf("next.\n"); } int fn2( ) { printf("executed "); } int fn3( ) { printf("is "); } int fn4( ) { printf("This "); } Output: This is executed first. This is executed next. This program pushes four functions onto the stack of functions to be exe- cuted when atexit is called. When the program exits, these programs are executed on a last-in, first-out basis. Standards conformance atexit is conformant with: ANSI X3.159-1989 Programming Language -- C.