atexit() Function
The atexit()
function is declared in the header file <stdlib.h>
.
The atexit()
function registers a function to be called automatically when the program terminates normally. This feature is useful for performing cleanup tasks, such as closing files or releasing resources, without having to call those functions explicitly in every exit path.
Syntax of atexit()
int atexit(void (*func)(void));
Parameters
Parameter | Description |
---|---|
func | A pointer to the function to be executed at program exit. This function must take no arguments and return no value. |
It is important to note that if multiple functions are registered using atexit()
, they are executed in the reverse order of their registration (i.e., last in, first out). Some implementations impose a limit on the number of functions that can be registered, but this is guaranteed to be no less than 32.
Return Value
The atexit()
function returns zero if the function was successfully registered. If it fails to register the function, a nonzero value is returned.
Examples for atexit()
Example 1: Registering a Single Exit Function
This example demonstrates how to register a single function that will be executed when the program terminates normally.
Program
#include <stdio.h>
#include <stdlib.h>
void goodbye() {
printf("Goodbye! Cleaning up resources...\n");
}
int main() {
if (atexit(goodbye) != 0) {
printf("Failed to register exit function.\n");
return 1;
}
printf("Program is running...\n");
return 0;
}
Explanation:
- A function
goodbye()
is defined to print a goodbye message. - The
atexit()
function registersgoodbye()
to be called upon normal program termination. - If registration fails, an error message is printed and the program exits with a nonzero status.
- When the program terminates normally, the
goodbye()
function is automatically invoked.
Program Output:
Program is running...
Goodbye! Cleaning up resources...
Example 2: Registering Multiple Exit Functions
This example illustrates how multiple functions can be registered with atexit()
and how they execute in the reverse order of registration.
Program
#include <stdio.h>
#include <stdlib.h>
void first() {
printf("First exit function called.\n");
}
void second() {
printf("Second exit function called.\n");
}
void third() {
printf("Third exit function called.\n");
}
int main() {
atexit(first);
atexit(second);
atexit(third);
printf("Main program execution...\n");
return 0;
}
Explanation:
- Three functions
first()
,second()
, andthird()
are defined, each printing a unique message. - The
atexit()
function registers these functions in the order:first
,second
, thenthird
. - Upon normal termination, the functions are executed in reverse order of registration:
third()
is called first, followed bysecond()
, and finallyfirst()
. - This demonstrates the stack-like behavior of exit function registration.
Program Output:
Main program execution...
Third exit function called.
Second exit function called.
First exit function called.
Example 3: Registering the Same Function Multiple Times
This example shows that the same function can be registered multiple times with atexit()
and will be executed as many times as it is registered, following the reverse order.
Program
#include <stdio.h>
#include <stdlib.h>
void cleanup() {
printf("Cleanup function called.\n");
}
int main() {
atexit(cleanup);
atexit(cleanup);
atexit(cleanup);
printf("Executing main function...\n");
return 0;
}
Explanation:
- The function
cleanup()
is defined to print a cleanup message. - The
cleanup()
function is registered three times usingatexit()
. - Upon program termination,
cleanup()
is invoked three times in reverse order of registration. - This confirms that registering the same function multiple times results in multiple calls during exit.
Program Output:
Executing main function...
Cleanup function called.
Cleanup function called.
Cleanup function called.