gtpc2m0fC/C++ Language Support User's Guide

atexit-Call a Function at Normal Program Termination

The atexit function records a function, pointed to by func that the TPF system calls at normal program termination. Normal program termination is the result of calling the exit function, returning from the main function, or calling the Basic Assembler Language (BAL) EXITC macro.

You can use the atexit function to register as many as 32 functions. The registered functions are called in reverse order. The registered functions must return to ensure that all registered functions are called.

Format

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

func
A pointer to a function to be called at normal program termination.

Normal Return

A zero value indicates that the function has completed successfully.

Error Return

A nonzero value indicates that the function has not completed successfully.

Programming Considerations

Examples

The following example uses the atexit function to call the goodbye() function at normal program termination.

#include <stdlib.h>
#include <stdio.h>
 
void goodbye(void);
 
int main(void)
{
   int rc;
 
   rc = atexit(goodbye);
   if (rc != 0)
      puts("Error in atexit.");
 
   return 0;
}
 
void goodbye(void)
{   /* This function is called at normal program termination. */
   puts("The goodbye() function was called at program termination.");
}

Output

The goodbye() function was called at program termination.

Related Information

exit-Exit an ECB.

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.