YouTip LogoYouTip

C Function Atexit

## C Library Function - atexit() The `atexit()` function is a standard C library function declared in ``. It allows you to register functions that will be automatically executed when the program terminates normally. This is highly useful for performing cleanup operations, such as closing files, releasing dynamically allocated memory, flushing buffers, or writing log entries before the application exits. --- ## Syntax ```c int atexit(void (*func)(void)); ``` ### Parameters * **`func`**: A pointer to the function to be called upon normal program termination. This function must take no arguments (`void`) and return nothing (`void`). ### Return Value * **`0`**: If the function is successfully registered. * **Non-zero value**: If the registration fails. --- ## How `atexit()` Works 1. **Registration**: You can register multiple cleanup functions by calling `atexit()` multiple times throughout your program. 2. **Execution Order**: Registered functions are called in the **reverse order** of their registration (Last-In, First-Out / LIFO). 3. **Trigger Conditions**: The registered functions are executed when: * The `main()` function returns. * The `exit()` function is explicitly called. 4. **Bypass Conditions**: The registered functions will **not** be called if: * The program terminates abnormally (e.g., via `abort()` or a segmentation fault). * The program is terminated via `_Exit()` or `_exit()`. --- ## Code Examples ### Example 1: Basic Usage The following example demonstrates how to register a single cleanup function using `atexit()`. ```c #include #include // Cleanup function to be registered void cleanupMessage() { printf("Cleanup: Program terminated successfully.\n"); } int main() { /* Register the termination function */ if (atexit(cleanupMessage) != 0) { fprintf(stderr, "Failed to register cleanup function.\n"); return 1; } printf("Starting the main program...\n"); printf("Exiting the main program...\n"); return 0; } ``` **Output:** ```text Starting the main program... Exiting the main program... Cleanup: Program terminated successfully. ``` --- ### Example 2: Multiple Registrations (LIFO Order) This example demonstrates how multiple registered functions are executed in reverse order of registration. ```c #include #include void cleanupA() { printf("Executing Cleanup A\n"); } void cleanupB() { printf("Executing Cleanup B\n"); } void cleanupC() { printf("Executing Cleanup C\n"); } int main() { // Registering functions in order: A, B, then C atexit(cleanupA); atexit(cleanupB); atexit(cleanupC); printf("Main program is running...\n"); printf("Main program is exiting...\n"); return 0; } ``` **Output:** ```text Main program is running... Main program is exiting... Executing Cleanup C Executing Cleanup B Executing Cleanup A ``` --- ## Important Considerations * **Limit on Registrations**: The C standard guarantees that you can register at least 32 functions using `atexit()`. Depending on the compiler and platform, the actual limit may be higher. * **No Arguments or Return Values**: The registered function must strictly match the signature `void func(void)`. You cannot pass arguments to it directly. * **Avoid Calling `exit()` Inside Registered Functions**: Calling `exit()` inside a function registered with `atexit()` can cause undefined behavior or infinite recursion. * **Static/Global Variables**: If a registered function accesses static or global variables, ensure those variables are still valid when the program exits.
← C Function ExitC Function Free β†’