summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2F_noreturn.html
blob: ff87fe14ab750ca7b7e255beaba612c9256a197b (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
52
53
54
55
56
57
58
59
60
61
    <h1 id="firstHeading" class="firstHeading">_Noreturn function specifier</h1>            <p>Specifies that the function does not return to its point of invocation.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin">  <tr class="t-sdsc"> <td> <code>_Noreturn</code> <span class="t-spar">function_declaration</span> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span><span class="t-mark-rev t-deprecated-c23">(deprecated in C23)</span> </td>
</tr> 
</table> <h3 id="Explanation"> Explanation</h3> <p>The <code>_Noreturn</code> keyword appears in a function declaration and specifies that the function does not return by executing the return statement or by reaching the end of the function body (it may return by executing <code><a href="../program/longjmp" title="c/program/longjmp">longjmp</a></code>). If the function declared <code>_Noreturn</code> returns, the behavior is undefined. A compiler diagnostic is recommended if this can be detected.</p>
<p>The <code>_Noreturn</code> specifier may appear more than once in the same function declaration, the behavior is the same as if it appeared once.</p>
<p>This specifier is typically used through the convenience macro <a href="../types" title="c/types"><code>noreturn</code></a>, which is provided in the header <code>&lt;stdnoreturn.h&gt;</code>.</p>
<table class="t-rev-begin"> <tr class="t-rev t-since-c23">
<td> <p><code>_Noreturn</code> function specifier is deprecated. <code>[[<a href="attributes/noreturn" title="c/language/attributes/noreturn">noreturn</a>]]</code> attribute should be used instead.</p>
<p>The macro <code>noreturn</code> is also deprecated.</p>
</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
</tr> </table> <h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/_noreturn" title="c/keyword/ Noreturn"><code>_Noreturn</code></a></p>
<h3 id="Standard_library"> Standard library</h3> <p>The following functions are <code>noreturn</code> in the standard library:</p>
<ul>
<li> <code><a href="../program/abort" title="c/program/abort">abort()</a></code> </li>
<li> <code><a href="../program/exit" title="c/program/exit">exit()</a></code> </li>
<li> <code><a href="../program/_exit" title="c/program/ Exit">_Exit()</a></code> </li>
<li> <code><a href="../program/quick_exit" title="c/program/quick exit">quick_exit()</a></code> </li>
<li> <code><a href="../thread/thrd_exit" title="c/thread/thrd exit">thrd_exit()</a></code> </li>
<li> <code><a href="../program/longjmp" title="c/program/longjmp">longjmp()</a></code> </li>
</ul> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdnoreturn.h&gt;
 
// causes undefined behavior if i &lt;= 0
// exits if i &gt; 0
noreturn void exit_now(int i) // or _Noreturn void exit_now(int i)
{
    if (i &gt; 0)
        exit(i);
}
 
int main(void)
{
    puts("Preparing to exit...");
    exit_now(2);
    puts("This code is never executed.");
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">Preparing to exit...</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul>
<li> 6.7.4 Function specifiers (p: TBD) </li>
<li> 7.23 _Noreturn &lt;stdnoreturn.h&gt; (p: TBD) </li>
</ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 6.7.4 Function specifiers (p: 90-91) </li>
<li> 7.23 _Noreturn &lt;stdnoreturn.h&gt; (p: 263) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 6.7.4 Function specifiers (p: 125-127) </li>
<li> 7.23 _Noreturn &lt;stdnoreturn.h&gt; (p: 361) </li>
</ul>
</ul>              <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <code>[[<a href="attributes/noreturn" title="c/language/attributes/noreturn">noreturn</a>]]</code><span class="t-mark-rev t-since-c23">(C23)</span><br><code>[[<a href="attributes/noreturn" title="c/language/attributes/noreturn">_Noreturn</a>]]</code><span class="t-mark-rev t-since-c23">(C23)</span><span class="t-mark">(deprecated)</span> </td> <td> indicates that the function does not return<br><span class="t-mark">(attribute specifier)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/attributes/noreturn" title="cpp/language/attributes/noreturn">C++ documentation</a></span> for <code>[[noreturn]]</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/language/_Noreturn" class="_attribution-link">https://en.cppreference.com/w/c/language/_Noreturn</a>
  </p>
</div>