diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/program%2Fatexit.html | |
new repository
Diffstat (limited to 'devdocs/c/program%2Fatexit.html')
| -rw-r--r-- | devdocs/c/program%2Fatexit.html | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/devdocs/c/program%2Fatexit.html b/devdocs/c/program%2Fatexit.html new file mode 100644 index 00000000..c9cebb8e --- /dev/null +++ b/devdocs/c/program%2Fatexit.html @@ -0,0 +1,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> |
