Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#include <stdlib.h>
int atexit(void (*func)(void));
Records a function, pointed to by func, that the system calls at normal program termination. Termination is a result of exit() or returning from main(), regardless of the language of the main() routine. Process termination started by _exit() or by a terminating signal under Language Environment is not included.
The functions are executed in the reverse order that they were registered. The registered function must return to ensure that all registered functions are called. The functions registered with atexit() are started before streams and files are closed. You may specify a number of functions to the limit set by the ATEXIT_MAX constant, which is defined in <limits.h>.
Under z/OS® UNIX services only, when a process ends, the address space is ended; otherwise, the address space persists.
Special behavior for XPG4.2: The maximum number of functions that can be registered is specified by the symbol ATEXIT_MAX which is defined in the limits.h header.
If successful, atexit() returns 0.
If unsuccessful, atexit() returns nonzero.
⁄* CELEBA10
This example uses the atexit() function to call the function goodbye()
at program termination.
*⁄
#include <stdlib.h>
#include <stdio.h>
#ifdef __cplusplus ⁄* the __cplusplus macro is *⁄
extern "C" void goodbye(void); ⁄* automatically defined by the *⁄
#else ⁄* C++⁄MVS compiler *⁄
void goodbye(void);
#endif
int main(void)
{
int rc;
rc = atexit(goodbye);
if (rc != 0)
printf("Error in atexit");
exit(0);
}
void goodbye(void)
⁄* This function is called at normal program termination *⁄
{
printf("The function goodbye was called \
at program termination\n");
}
The function goodbye was called at program termination