Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

atexit - Register termination routines

&pagelevel(4)&pagelevel

Definition

#include <stdlib.h>

int atexit(void (*funct) (void));

atexit is used to register a function funct that is to be executed when the program terminates
normally.

Return val.

0

!= 0

on successful registration of the function.

on error.

Notes

Up to 40 functions can be registered. The functions are called in the reverse order of their
registration. If a function is registered more than once it is also called more than once.

The functions registered with atexit are only called if the program is terminated “normally”
in one of the following ways:

  • by explicitly calling the exit function

  • on termination of the main function without an explicit exit call

  • on termination of the program by the C runtime system with exit(-1), in other words:
    on the occurrence of a raise signal (not SIGABRT) which is either not processed or is
    processed by the signal default function SIG_DFL (see signal).

Only when all the termination routines have been processed are any files still open
automatically closed.

Example

The termination routines end1 and end2 are registered with atexit and executed in the
order end2, end1 when the main function terminates.

#include <stdlib.h>
#include <stdio.h>
void end1(void);
void end2(void);
int main(void)
{
   atexit(end1);
   atexit(end2);
   printf("main function\n");
   return 0;
}
void end1(void)
{
   printf("end1 routine\n");
}
void end2(void)
{
   printf("end2 routine\n");
}

See also

exit, raise, signal