VOOZH about

URL: https://www.geeksforgeeks.org/cpp/atexit-function-in-c-c/

⇱ atexit() function in C/C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

atexit() function in C/C++

Last Updated : 11 Jul, 2025

The function pointed by atexit() is automatically called without arguments when the program terminates normally. In case more than one function has been specified by different calls to the atexit() 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. 
Syntax : 
 

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 func which specifies the pointer to the function to be called on normal program termination(Function to be called). 
Return Value : The function returns following values: 
 

  • Zero, if the function registration is successful
  • Non zero, if the function registration failed


Below programs illustrate the above-mentioned function:
Program 1: 
 


Output: 
Registration successful
Exiting Successfully

 

If atexit function is called more than once, then all the specified functions will be executed in a reverse manner, same as of the functioning of the stack. 
Program 2: 
 


Output: 
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. 
 

Comment
Article Tags: