[Previous]
[Contents]
[Next]

atexit()

register functions to be called when the program terminates normally

Synopsis:

#include <stdlib.h>
int atexit( void (*func)(void) );

Description:

The atexit() function is passed the address of function func to be called when the program terminates normally. Successive calls to atexit() create a list of functions that will be executed on a "last-in, first-out" basis. No more than 32 functions can be registered with the atexit() function.

The functions have no parameters and don't return values.


Note:
  • The functions registered with atexit() aren't called when the program is terminated by a call to _exit().
  • The ANSI atexit() function does the same thing as the non-ANSI onexit() function. Use atexit() when ANSI portability is a concern.

Returns:

0
if the registration succeeds
Nonzero
if it fails

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( void )
  {
    void func1(void), func2(void), func3(void);

    atexit( func1 );
    atexit( func2 );
    atexit( func3 );
    printf( "Do this first.\n" );
    return( EXIT_SUCCESS );
  }

void func1(void) { printf( "last.\n" ); }

void func2(void) { printf( "this " ); }

void func3(void) { printf( "Do " ); }

produces the output:

Do this first.
Do this last.

Classification:

ANSI

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

abort(), _exit(), exit()


[Previous]
[Contents]
[Next]