1<html lang="en"> 2<head> 3<title>atexit - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Stdlib.html#Stdlib" title="Stdlib"> 9<link rel="prev" href="assert.html#assert" title="assert"> 10<link rel="next" href="atof.html#atof" title="atof"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<meta http-equiv="Content-Style-Type" content="text/css"> 13<style type="text/css"><!-- 14 pre.display { font-family:inherit } 15 pre.format { font-family:inherit } 16 pre.smalldisplay { font-family:inherit; font-size:smaller } 17 pre.smallformat { font-family:inherit; font-size:smaller } 18 pre.smallexample { font-size:smaller } 19 pre.smalllisp { font-size:smaller } 20 span.sc { font-variant:small-caps } 21 span.roman { font-family:serif; font-weight:normal; } 22 span.sansserif { font-family:sans-serif; font-weight:normal; } 23--></style> 24<link rel="stylesheet" type="text/css" href="../cs.css"> 25</head> 26<body> 27<div class="node"> 28<a name="atexit"></a> 29<p> 30Next: <a rel="next" accesskey="n" href="atof.html#atof">atof</a>, 31Previous: <a rel="previous" accesskey="p" href="assert.html#assert">assert</a>, 32Up: <a rel="up" accesskey="u" href="Stdlib.html#Stdlib">Stdlib</a> 33<hr> 34</div> 35 36<h3 class="section">2.6 <code>atexit</code>—request execution of functions at program exit</h3> 37 38<p><a name="index-atexit-7"></a><strong>Synopsis</strong> 39<pre class="example"> #include <stdlib.h> 40 int atexit (void (*<var>function</var>)(void)); 41 42</pre> 43 <p><strong>Description</strong><br> 44You can use <code>atexit</code> to enroll functions in a list of functions that 45will be called when your program terminates normally. The argument is 46a pointer to a user-defined function (which must not require arguments and 47must not return a result). 48 49 <p>The functions are kept in a LIFO stack; that is, the last function 50enrolled by <code>atexit</code> will be the first to execute when your program 51exits. 52 53 <p>There is no built-in limit to the number of functions you can enroll 54in this list; however, after every group of 32 functions is enrolled, 55<code>atexit</code> will call <code>malloc</code> to get space for the next part of the 56list. The initial list of 32 functions is statically allocated, so 57you can always count on at least that many slots available. 58 59 <p><br> 60<strong>Returns</strong><br> 61<code>atexit</code> returns <code>0</code> if it succeeds in enrolling your function, 62<code>-1</code> if it fails (possible only if no space was available for 63<code>malloc</code> to extend the list of functions). 64 65 <p><br> 66<strong>Portability</strong><br> 67<code>atexit</code> is required by the ANSI standard, which also specifies that 68implementations must support enrolling at least 32 functions. 69 70 <p>Supporting OS subroutines required: <code>close</code>, <code>fstat</code>, <code>isatty</code>, 71<code>lseek</code>, <code>read</code>, <code>sbrk</code>, <code>write</code>. 72 73 <p><br> 74 75 </body></html> 76 77