C library function - atexit()


The C library function int atexit(void (*func)(void)) causes the specified function func to be called when the program terminates. You can register your termination function anywhere you like, but it will be called at the time of the program termination.


Following is the declaration for atexit() function.

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


  • func − This is the function to be called at the termination of the program.

Return Value

This function returns a zero value if the function is registered successfully, otherwise a non-zero value is returned if it is failed.


The following example shows the usage of atexit() function.

#include <stdio.h>
#include <stdlib.h>

void functionA () {
   printf("This is functionA\n");

int main () {
   /* register the termination function */
   atexit(functionA );
   printf("Starting  main program...\n");

   printf("Exiting main program...\n");


Let us compile and run the above program that will produce the following result −

Starting main program...
Exiting main program...
This is functionA