English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C / C ++atexit() function in

This functionatexit()Used to call this function after the program exits normally. No parameters are required to call this function. This functionatexit()is called afterexit(). The termination function can be called at any location in the program. This function is declared in the "stdlib.h" header file.

Thisatexit()C Language Syntax,

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

Here,

function_name-This function will be called when the program terminates.

Thisatexit()C Language Example,

Example

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

Output Result

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

In the above program, inmain()Two functions func1and func2 . By usingatexit()Functions defined are called. Thismain()Function is called before the function exits.main()Functions. We called two functions as follows.

atexit(func1);
printf("\nStarting of main()\n");
atexit(func2);
printf("\nEnding of main()\n");