atexit() in C++

atexit(): This function is available in the file stdlib.h. This function registers the function pointed to by func to be called on normal program termination (via std::exit() or returning from the main function) The same function may be registered more than once. If a function exits via an exception, std::terminate is called. 

It is a thread-safe: calling the function from several threads does not induce a data race. 

Note: The implementation is guaranteed to support the registration of at least 32 functions. The exact limit is implementation-defined. 

Parameters: One parameter is required for this function.

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

This function returns value ​0​ if the registration succeeds, nonzero value otherwise.

Syntax:

atexit(func)

For Example:

atexit(fun)

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void fun()
{
    cout << "Atexit\n";
}
int main()
{
    int x = atexit(fun);
    if (x == 0)
        cout << "Succesfull\n";
    else
        cout << "Not Succesfull\n";

    

    return 0;
}


No comments:

Post a Comment