atexit() — Record Program Ending Function

Format

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

Language Level

ANSI

Threadsafe

Yes

Description

The atexit() function records the function, pointed to by func, that the system calls at normal program end. For portability, you should use the atexit() function to register a maximum of 32 functions. The functions are processed in a last-in, first-out order. The atexit() function cannot be called from the OPM default activation group. Most functions can be used with the atexit function; however, if the exit function is used the atexit function will fail.

Return Value

The atexit() function returns 0 if it is successful, and nonzero if it fails.

Example

This example uses the atexit() function to call goodbye() at program end.
#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
   void goodbye(void);
   int rc;
 
   rc = atexit(goodbye);
   if (rc != 0)
      perror("Error in atexit");
   exit(0);
}
 
void goodbye(void)
   /* This function is called at normal program end */
{
   printf("The function goodbye was called at program end\n");
}
 
/****************  Output should be similar to:  ******************
 
The function goodbye was called at program end
*/

Related Information