Is it possible to call an extra function when main() exits in C?
Thanks!
You can register functions to run after main exits using the atexit function.
MSDN has a nice succinct example of how this is done. Basically, the functions registered with atexit are executed in reverse order of when they were registered.
atexit, being careless about how you call exit is dangerous. It's not async-signal-safe, and since it flushes all open files, it's almost sure to give you problems if you break the rules about calling it.While atexit() is the standard for registering a function to run at process termination, GCC provides a destructor function attribute* that causes a function to be called automatically when main() has completed or exit() has been called.
void __attribute__ ((destructor)) my_fini(void); * GCC specific