atexit function in c

atexit function in C/C++, you should know

The atexit function registers the function pointed to by func and this registered function called without arguments when the program terminates normally.

In case of more than one atexit function, the registered functions are called in the reverse order of their registration (i.e. the last function specified is the first to be executed at exit).

Syntax atexit in C:

//Syntax of atexit

int atexit(void (*func)(void));

Parameters:

func – pointer to a function to be called on normal program termination

Return:

The atexit function returns zero if the registration succeeds, nonzero if it fails. It means,

  • Zero if the function registration is successful.
  • Non zero if the function registration failed.

 

The following example demonstrates the use of the atexit function.

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

void Testfunction ()
{
    printf("This is Testfunction\n");
}

int main ()
{
    int status;

    //register the termination function
    status = atexit(Testfunction);
    if (status != 0)
    {
        printf("Registration Failed\n");
        exit(1);
    }

    printf("Registration successful\n");

    return 0;
}

Let us compile and run the above program. If the atexit function registers the function “Testfunction” successfully, then it calls Testfunction after normal termination.

atexit in c

 

 

Some important points related to atexit() function in C:

1. You must include stdlib.h header file before using the atexit function in C.

2. We can register more than one function to execute on termination.

3. If more than one atexit functions are registered, they are executed in the reverse order. It means the function registered at last is executed at first. Let’s see the example code.

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

void Testfunction1()
{
    printf("This is Testfunction1\n");
}

void Testfunction2()
{
    printf("This is Testfunction2\n");
}


void Testfunction3()
{
    printf("This is Testfunction3\n");
}


int main ()
{
    int status;

    //register the termination functions
    //if successfully register return 0
    status = atexit(Testfunction1);
    status = (status == 0)? atexit(Testfunction2):1;
    status = (status == 0)? atexit(Testfunction3):1;

    if (status != 0)
    {
        printf("Registration Failed\n");
        exit(1);
    }

    printf("Registration successful\n\n");

    return 0;
}

Let us compile and run the above program. If the atexit function registers all three functions “Testfunction1”, “Testfunction2”, “Testfunction3”,successfully, then it calls the all three function after normal termination.

more than one atexit call in c

 

4. The same function can be registered more than once. Let’s see the example code.

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

void Testfunction1()
{
    printf("This is Testfunction1\n");
}


int main ()
{
    int status;

    //register the termination functions
    //if successfully register return 0
    status = atexit(Testfunction1);
    status = (status == 0)? atexit(Testfunction1):1;
    status = (status == 0)? atexit(Testfunction1):1;

    if (status != 0)
    {
        printf("Registration Failed\n");
        exit(1);
    }

    printf("Registration successful\n\n");

    return 0;
}

When you run the program, the output will be:

Registration successful

This is Testfunction1
This is Testfunction1
This is Testfunction1

 

5. The number of functions calls that can be registered with atexit() depends on the particular library implementation. However, the minimum limit is 32.

6. If a registered function throws an unhandled exception when called on termination, the terminate() function is called (C++).

#include <iostream>
#include <cstdlib>
using namespace std;

void Testfunction()
{
  cout << "Generates Exception";
  int a = 5, b = 0;
  int x = a/b;
  /* Program will terminate here */
  cout << "Division by zero";
}

int main()
{
  int status;
  
  status = atexit(Testfunction);
  if (status != 0)
  {
    cout << "Registration Failed";
    exit(1);
  }
  cout << "Registration successful" << endl;

  return 0;
}

Output:

Registration successful
Generates ExceptionDivision by zero

 

Recommended Articles for you: