The function pointed by atexit() is automatically called without arguments when the program terminates normally. In case more than one atexit function has been specified by different calls to this function, all are executed in the order of 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.
extern "C" int atexit (void (*func)(void)) noexcept; extern "C++" int atexit (void (*func)(void)) noexcept
Note: extern refers that the name will refer, to the same object in the entire program .
Parameters : The function accepts a single mandatory parameter fun which specifies the pointer to the function to be called on normal program termination(Function to be called).
Return Value : The function returns two values:
- Zero, if the function registration is successful
- Non zero, if the function registration failed
Below programs illustrates the above-mentioned function:
Registration successful Exiting Successfully
If more than one atexit function have been called in the main, then all the specified function will be executed in a reverse manner, same as of the functioning of the stack.
Registration successful Exit Fourth Exit Third Exit Second Exit first
Program 3 :
Note: If a registered function throws an exception which cannot be handled, then the terminate() function is called.
- Function Overloading vs Function Overriding in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- fma() function in C++
- arc function in C
- div() function in C++
- log() function in C++
- exp() function C++
- wcstoul() function in C/C++
- fegetexceptflag() function in C/C++
- vswprintf() function in C/C++
- wcrtomb() function in C/C++
- wmemchr() function in C/C++
- wmemcmp() function in C/C++
- wctob() function in C++
- Modulus function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.