Previous Page
Next Page


Registers a function to be called when the program exits

#include <stdlib.h>
int atexit ( 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.


int main( )
  void f1(void), f2(void);

  printf("Registering the \"at-exit\" functions f1 and f2:");

  if ( atexit(f1) || atexit(f2) )
    printf(" failed.\n");
    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( ).

See Also

_Exit( ), exit( ), abort( )

Previous Page
Next Page