C library function - atexit()



Description

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.

Declaration

Following is the declaration for atexit() function.

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

Parameters

  • 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.

Example

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");

   return(0);
}

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

Starting main program...
Exiting main program...
This is functionA
stdlib_h.htm
Advertisements