[Previous] [Contents] [Next]

atexit()

Register functions to be called during normal program termination

Synopsis:

#include <stdlib.h>

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

Library:

libc

Description:

The atexit() function registers the function pointed to by func, that's called without arguments when the program terminates normally. If more than one function is registered with atexit(), they're executed in a "last-in, first-out" basis. Normal termination occurs either by a call to exit() or a return from main().

A total of 32 functions can be registered with atexit().

The func function has no parameters and doesn't return a value; its prototype should be:

void func( void );

Note: The functions registered with atexit() aren't called when the program terminates with a call to _exit().

Returns:

0 for success, or nonzero if an error occurs.

Examples:

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

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

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

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

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

    printf( "Do this first.\n" );

    return EXIT_SUCCESS;
}

produces the output:

Do this first.
Do this last.

Classification:

ANSI

Safety:
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes

See also:

abort(), _exit(), exit()


[Previous] [Contents] [Next]