
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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()");
- Related Articles
- Write a C program to check the atexit() function
- Python Program Exit handlers (atexit)
- iswblank() function in C/C++
- iswpunct() function in C/C++
- strchr() function in C/C++
- strtod() function in C/C++
- memmove() function in C/C++
- memcpy() function in C/C++
- raise() function in C/C++
- mbrlen() function in C/C++
- strftime() function in C/C++
- iswlower() function in C/C++
- towupper() function in C/C++
- iswdigit() function in C/C++
- mbrtowc() function in C/C++

Advertisements