blob: a7ae5e4f3435c91b7eb16ebde5112f9726b9941e (
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
51
|
<h1 id="firstHeading" class="firstHeading">exit</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 t-until-c11"> <td><pre data-language="c">void exit( int exit_code );</pre></td> <td class="t-dcl-nopad"> </td> <td><span class="t-mark-rev t-until-c11">(until C11)</span></td> </tr> <tr class="t-dcl t-since-c11 t-until-c23"> <td> <pre data-language="c">_Noreturn void exit( int exit_code );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> <br><span class="t-mark-rev t-until-c23">(until C23)</span> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">[[noreturn]] void exit( int exit_code );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> </table> <p>Causes normal program termination to occur.</p>
<p>Several cleanup steps are performed:</p>
<ul>
<li> functions passed to <code><a href="atexit" title="c/program/atexit">atexit</a></code> are called, in reverse order of registration </li>
<li> all C streams are flushed and closed </li>
<li> files created by <code><a href="../io/tmpfile" title="c/io/tmpfile">tmpfile</a></code> are removed </li>
<li> control is returned to the host environment. If <code>exit_code</code> is zero or <code><a href="exit_status" title="c/program/EXIT status">EXIT_SUCCESS</a></code>, an implementation-defined status indicating successful termination is returned. If <code>exit_code</code> is <code><a href="exit_status" title="c/program/EXIT status">EXIT_FAILURE</a></code>, an implementation-defined status indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned. </li>
</ul> <h3 id="Notes"> Notes</h3> <p>The functions registered with <code><a href="at_quick_exit" title="c/program/at quick exit">at_quick_exit</a></code> are not called.</p>
<p>The behavior is undefined if a program calls <code>exit</code> more than once, or if it calls <code>exit</code> and <code><a href="quick_exit" title="c/program/quick exit">quick_exit</a></code></p>
<p>The behavior is undefined if during a call to a function registered with <code><a href="atexit" title="c/program/atexit">atexit</a></code>, the function exits with <code><a href="longjmp" title="c/program/longjmp">longjmp</a></code>.</p>
<p>Returning from the <a href="../language/main_function" title="c/language/main function">the main function</a>, either by a <code>return</code> statement or by reaching the end of the function, executes <code>exit()</code>, passing the argument of the return statement (or <code>0</code> if implicit return was used) as <code>exit_code</code>.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> exit_code </td> <td> - </td> <td> exit status of the program </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>(none)</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = fopen("data.txt","r");
if (fp == NULL)
{
fprintf(stderr, "error opening file data.txt in function main()\n");
exit( EXIT_FAILURE );
}
fclose(fp);
printf("Normal Return\n");
return EXIT_SUCCESS ;
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">error opening file data.txt in function main()</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.22.4.4 The exit function (p: 256) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.22.4.4 The exit function (p: 351-352) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.4.3 The exit function (p: 315-316) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.10.4.3 The exit function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="abort" title="c/program/abort"> <span class="t-lines"><span>abort</span></span></a></div> </td> <td> causes abnormal program termination (without cleaning up) <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="atexit" title="c/program/atexit"> <span class="t-lines"><span>atexit</span></span></a></div> </td> <td> registers a function to be called on <code>exit()</code> invocation <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="quick_exit" title="c/program/quick exit"> <span class="t-lines"><span>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> causes normal program termination without completely cleaning up <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/exit" title="cpp/utility/program/exit">C++ documentation</a></span> for <code>exit</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/exit" class="_attribution-link">https://en.cppreference.com/w/c/program/exit</a>
</p>
</div>
|