
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
atexit Function in C/C++
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))
Here,
function_name − The function is to be called at the time of termination of program.
Here is an example of atexit() in C language,
Example
#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; }
Output
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()");
Advertisements