int atexit (void (*func)(void));
extern "C" int atexit (void (*func)(void));
extern "C++" int atexit (void (*func)(void));
extern "C" int atexit (void (*func)(void)) noexcept;
extern "C++" int atexit (void (*func)(void)) noexcept;
Set function to be executed on exit
The function pointed by func is automatically called without arguments when the program terminates normally.
If more than one atexit function has been specified by different calls to this function, they are all executed in reverse order as a stack (i.e. the last function specified is the first to be executed at exit).
A single function can be registered to be executed at exit more than once.
If atexit is called after exit, the call may or may not succeed depending on the particular system and library implementation (unspecified behavior).
If a function registered with atexit throws an exception for which it does not provide a handler when called on termination, terminate is automatically called (C++).
Particular library implementations may impose a limit on the number of functions call that can be registered with atexit, but this cannot be less than 32 function calls.
- Function to be called. The function shall return no value and take no arguments.
A zero value is returned if the function was successfully registered.
If it failed, a non-zero value is returned.
/* atexit example */
#include <stdio.h> /* puts */
#include <stdlib.h> /* atexit */
void fnExit1 (void)
puts ("Exit function 1.");
void fnExit2 (void)
puts ("Exit function 2.");
int main ()
puts ("Main function.");
Exit function 2.
Exit function 1.
Concurrently calling this function introduces no data races (C++): Calls are properly synchronized at the process level.
No-throw guarantee: this function never throws exceptions.
- Terminate calling process (function
- Abort current process (function