function

atexit

<cstdlib>
extern "C" int atexit ( void ( * func ) (void) ) noexcept;
extern "C++" int atexit ( void ( * func ) (void) ) noexcept;
Set function to be executed on exit
The function pointed by func is automatically called without arguments when the program terminates normally.

If more than one atexit function has been specified by different calls to this function, they are all executed in reverse order as a stack (i.e. the last function specified is the first to be executed at exit).

A single function can be registered to be executed at exit more than once.

If atexit is called after exit, the call may or may not succeed depending on the particular system and library implementation (unspecified behavior).

If a function registered with atexit throws an exception for which it does not provide a handler, terminate is automatically called (C++).

No data races are introduced by this function (C++).

Particular library implementations may impose a limit on the number of functions that can be registered with atexit, but this cannot be less than 32 functions.

Parameters

function
Function to be called. The function shall return no value and take no arguments.

Return Value

A zero value is returned if the function was successfully registered.
If it failed, a non-zero value is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* atexit example */
#include <stdio.h>
#include <stdlib.h>

void fnExit1 (void)
{
  puts ("Exit function 1.");
}

void fnExit2 (void)
{
  puts ("Exit function 2.");
}

int main ()
{
  atexit (fnExit1);
  atexit (fnExit2);
  puts ("Main function.");
  return 0;
}


Output:

Main function.
Exit function 2.
Exit function 1.

See also