create a list of functions to be called when the program terminates normally


#include <stdlib.h>
onexit_t onexit( onexit_t func );


The onexit() function is passed the address of function func to be called when the program terminates normally. Successive calls to onexit() 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 onexit() function.

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

  • The functions registered with onexit() aren't called when the program is terminated by a call to _exit().
  • The onexit() function isn't an ANSI function. The ANSI standard function atexit() does the same thing as onexit(), and should be used instead when ANSI portability is a concern.


func if the registration succeeds, and NULL if it fails.


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

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

    onexit( func1 );
    onexit( func2 );
    onexit( 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.



Interrupt handler No
Signal handler No
Thread Yes

See also:

abort(), atexit(), exit(), _exit()