blob: c9cebb8e33e1fa49793e3f5acfd0ed5c338eddf2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<h1 id="firstHeading" class="firstHeading">atexit</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">int atexit( void (*func)(void) );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Registers the function pointed to by <code>func</code> to be called on normal program termination (via <code><a href="exit" title="c/program/exit">exit()</a></code> or returning from <code>main()</code>). The functions will be called in reverse order they were registered, i.e. the function registered last will be executed first.</p>
<p>The same function may be registered more than once.</p>
<p><code>atexit</code> is thread-safe: calling the function from several threads does not induce a data race.</p>
<p>The implementation is guaranteed to support the registration of at least <code>32</code> functions. The exact limit is implementation-defined.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> func </td> <td> - </td> <td> pointer to a function to be called on normal program termination </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p><code>0</code> if the registration succeeds, nonzero value otherwise.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdlib.h>
#include <stdio.h>
void f1(void)
{
puts("f1");
}
void f2(void)
{
puts("f2");
}
int main(void)
{
if ( ! atexit(f1) && ! atexit(f2) && ! atexit(f2) )
return EXIT_SUCCESS ;
// atexit registration failed
return EXIT_FAILURE ;
} // <- if registration was successful calls f2, f2, f1</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">f2
f2
f1</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.22.4.2 The atexit function (p: 255) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.22.4.2 The atexit function (p: 350) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.4.2 The atexit function (p: 315) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 7.10.4.2 The atexit function (p: 156) </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="at_quick_exit" title="c/program/at quick exit"> <span class="t-lines"><span>at_quick_exit</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> registers a function to be called on <a href="quick_exit" title="c/program/quick exit"><code>quick_exit</code></a> invocation <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/utility/program/atexit" title="cpp/utility/program/atexit">C++ documentation</a></span> for <code>atexit</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/program/atexit" class="_attribution-link">https://en.cppreference.com/w/c/program/atexit</a>
</p>
</div>
|