Register functions to be called during normal program termination
#include <stdlib.h> int atexit( void (*func)(void) );
void func( void );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The atexit() function registers a function to be called when the program terminates normally. If you register more than one function with atexit(), they're executed in a "last-in, first-out" order. Normal termination occurs either by a call to exit() or a return from main().
You can register a total of 32 functions with atexit().
0 for success, or nonzero if an error occurs.
#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.
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | No |
| Signal handler | No |
| Thread | Yes |