atexit()

Prototype: int atexit(void (_USERENTRY * func)(void));
Header File: stdlib.h (C) or cstdlib (C++)
ANSI: C and C++
Explanation: Use atexit to have a function called when the program exits. The parameter it accepts is a function name, without the typical () of a function call. It must return void and accept no values (hence the use of void in the prototype of atexit). If atexit is called more than once, the last function passed to it will be the first one executed. It can be used up to 32 times.



Example:
#include <iostream>
#include <cstdlib>

using namespace std;

//Program calls atexit with FinalFunction
//FinalFunction is executed before program ends
void FinalFunction(void)
{
    cout<<"Final function called";
}
int main()
{
  atexit(FinalFunction);
}
Other Functions