atexit() function in C/C++

CC++Server Side Programming

The function atexit() is used to call the function after the normal exit of program. The program is called without any parameters. The function atexit() is called after exit(). The termination function can be called anywhere in the program. This function is declared in “stdlib.h” header file.

Here is the syntax of atexit() in C language,

int atexit(void (*function_name)(void))


function_name − The function is to be called at the time of termination of program.

Here is an example of atexit() in C language,


 Live Demo

#include <stdio.h>
#include <stdlib.h>
void func1 (void) {
   printf("\nExit of function 1");
void func2 (void) {
   printf("\nExit of function 2");
int main () {
   atexit (func1);
   printf("\nStarting of main()");
   atexit (func2);
   printf("\nEnding of main()");
   return 0;


Starting of main()
Ending of main()
Exit of function 2
Exit of function 1

In the above program, two functions func1 and func2 are defined before main() function. By using atexit(), defined functions are called. The main() function calls the functions before the exit of main() function. We called the two functions as shown below.

atexit (func1);
printf("\nStarting of main()");
atexit (func2);
printf("\nEnding of main()");
Published on 05-Nov-2018 12:05:19