C Standard Libraries C++

atexit()

Declaration

int atexit(void (*pfnFunctionPtr)( void ));

Description

Adds a function to the call stack of functions to be called when the program exists. The function, atexit(), takes a function pointer as its argument. It returns 0 if the sucessful, or a nonzero result if it fails. The argument function pointer type takes a void and returns a void.

Example

#include <iostream>
#include <cstdlib>

void First() {
    std::cout << "1" << std::endl;
}

void Second() {
    std::cout << "2" << std::endl;
}

void Third() {
    std::cout << "3" << std::endl;
}

int main() {
    using namespace std;

    atexit(First);
    atexit(Second);
    atexit(Third);

    cout << "The functions are called when we return" << endl;
    cout << "and in reverse order." << endl;
    return 0;
}

Output

atexit() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.