C Reference function atexit()

With the function atexit() it is possible to set a function that will be executed on termination of the program.

Usage of atexit():

int atexit ( void ( * func ) (void) );

If the program is terminated normally then the function pointed by the func pointer argument is called.
If there are more then one atexit functions (multiple calls) then they are all executed in reverse order as the where stacked.
(Which means that the last function specified is first to execute at exit of the program.)
Note: one single function can be executed more then once on exit.


Function that needs to be called.

Note: the function cannot return an argument and the function cannot accept arguments.

Return value:

A non zero is returned if it fails and a zero is returned if it was successful.

Source code example of atexit():


	void testfunction1 (void)
		puts ("message function 1");

	void testfunction2 (void)
		puts ("message function 2");

	int main ()
		atexit (testfunction1);
		atexit (testfunction2);
		puts ("message from main function.");
		return 0;

This entry was posted in C Reference stdlib.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.