atexit, _fatexit

Description

Process the specified function at exit.

#include <stdlib.h> Required only for function declarations  

int atexit( void ( __cdecl *func )( void ) );

int __far _fatexit( void ( __cdecl __far *func )( void ) );

func Function to be called  

Remarks

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 in LIFO (last-in-first-out) order. No more than 32 functions can be registered with atexit or _onexit. The functions passed to atexit cannot take parameters.

For DOS32X, atexit and _onexit use the heap to hold the “register of functions.” Thus, the number of functions that can be registered is limited only by heap memory.

The _fatexit function is a far version of atexit; it can be used with any memory model.

Return Value

Both atexit and _fatexit return 0 if successful, or a nonzero value if an error occurs (e.g., if there are already 32 exit functions defined).

Compatibility

atexit

Standards:ANSI

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

Use the ANSI-standard atexit function (rather than the similar _onexit function) whenever ANSI portability is desired._fatexit

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:None

See Also

abort, exit, _exit, _onexit

Example

/* ATEXIT.C: This program pushes four functions onto the stack of functions

* to be executed when atexit is called. When the program exits, these

* programs are executed on a "last in, first out" basis.

*/

#include <stdlib.h>

#include <stdio.h>

void fn1( void ), fn2( void ), fn3( void ), fn4( void );

void main( void )

{

atexit( fn1 );

atexit( fn2 );

atexit( fn3 );

atexit( fn4 );

printf( "This is executed first.\n" );

}

void fn1()

{

printf( "next.\n" );

}

void fn2()

{

printf( "executed " );

}

void fn3()

{

printf( "is " );

}

void fn4()

{

printf( "This " );

}

Output

This is executed first.

This is executed next.