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++
- How to call some function before main() function in C++?
- Difference between Virtual function and Pure virtual function in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- fma() function in C++
- log() function in C++
- exp() function C++
- div() function in C++
- arc function in C
- exp2() function in C++ STL
- strtol() function in C++ STL
- iswxdigit() function in C/C++
- towlower() function in C/C++
- strrev() function in C
- array get() 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 email@example.com. 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.