C library - atexit() function



The C stdlib library atexit() function causes the specified function "func" to be called when the program terminates normally. We can register our termination function anywhere, but it will be called at the time of the program termination.

When multiple functions are specified by different calls to the atexit() function, they are executed in the order of a stack.

This function is typically useful for performing clean-up tasks such as saving state, releasing resources, or displaying a message before the program exits.

Syntax

Following is the C library syntax of the atexit() function −

int atatexit(void (*func)(void))

Parameters

This function accepts a single parameter −

  • func − It represents the pointer to a function to be called on normal program termination.

Return Value

This function returns following value −

  • zero − If the function registration is successful.
  • non zero − If the function registration is fails.

Example 1

In this example, we create a basic c program to demonstrate the use of atexit() function.

#include <stdio.h>
#include <stdlib.h>

void tutorialspoint(void) {
   printf("tutorialspoint.com India\n");
}

int main() {
   if (atexit(tutorialspoint) != 0) {
      fprintf(stderr, "Failed to register the tutorialspoint function with atexit\n");
      return EXIT_FAILURE;
   }
   
   printf("This will be printed before the program exits.\n");
   return EXIT_SUCCESS;
}

Output

Following is the output −

This will be printed before the program exits.
tutorialspoint.com India

Example 2

Let's create another c program, where atexit() function called more than once. So, all specified function executed in the order of stack.

#include <stdio.h>
#include <stdlib.h>

// first function
void first() {
   printf("Exit 1st \n");
}
// second function
void second() {
   printf("Exit 2nd \n");
}
// third function
void third() {
   printf("Exit 3rd \n");
}
// main function
int main() {
   int value1, value2, value3;
   value1 = atexit(first);
   value2 = atexit(second);
   value3 = atexit(third);
   if ((value1 != 0) || (value2 != 0) || (value3 != 0)) {
      printf("registration Failed for atexit() function. \n");
	  exit(1);
   }
   // it will execute at the first
   printf("Registration successful \n");
   return 0;
}

Output

Following is the output −

Registration successful
Exit 3rd
Exit 2nd
Exit 1st

Example 3

Here, we create a C program that performs addition and subtraction of two numbers and displays the results of these functions in the stack manner using the atexit().

#include <stdio.h>
#include <stdlib.h>

// first function
void add() {
   int a=10;
   int b=5;
   int res = a+b;
   printf("addition of two number is: %d\n", res);
}
// second function
void sub() {
   int a=10;
   int b=5;
   int res = a-b;
   printf("subtraction of two number is: %d\n", res);
}
// main function
int main() {
   int value1, value2;
   value1 = atexit(add);
   value2 = atexit(sub);
   if ((value1 != 0) || (value2 != 0)) {
      printf("registration Failed for atexit() function. \n");
      exit(1);
   }
   // it will execute at the first
   printf("calculation successful \n");
   return 0;
}

Output

Following is the output −

calculation successful
subtraction of two number is: 5
addition of two number is: 15
Advertisements