summaryrefslogtreecommitdiff
path: root/devdocs/c/error%2Ferrno.html
blob: 687e991b87411a107b75c3d4edde5c08bcf8f159 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    <h1 id="firstHeading" class="firstHeading">errno</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;errno.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">#define errno /*implementation-defined*/</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p><code>errno</code> is a preprocessor macro (but see note below) that expands to a <span class="t-rev-inl t-since-c11"><span>thread-local</span><span><span class="t-mark-rev t-since-c11">(since C11)</span></span></span> modifiable lvalue of type <code>int</code>. Several standard library functions indicate errors by writing positive integers to <code>errno</code>. Typically, the value of <code>errno</code> is set to one of the error codes listed in <code>&lt;errno.h&gt;</code> as macro constants beginning with the letter <code>E</code> followed by uppercase letters or digits.</p>
<p>The value of <code>errno</code> is <code>​0​</code> at program startup, and although library functions are allowed to write positive integers to <code>errno</code> whether or not an error occurred, library functions never store <code>​0​</code> in <code>errno</code>.</p>
<p>Library functions <code><a href="../io/perror" title="c/io/perror">perror</a></code> and <code><a href="../string/byte/strerror" title="c/string/byte/strerror">strerror</a></code> can be used to obtain textual descriptions of the error conditions that correspond to the current <code>errno</code> value.</p>
<p>Note: Until C11, the C standards had contradictory requirements, in that they said that <code>errno</code> is a macro but <i>also</i> that "it is unspecified whether <code>errno</code> is a macro or an identifier declared with external linkage". C11 fixes this, requiring that it be defined as a macro (see also WG14 <a rel="nofollow" class="external text" href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1338.htm">N1338</a>).</p>
<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;math.h&gt;
#include &lt;errno.h&gt;
 
void show_errno(void)
{
    const char *err_info = "unknown error";
    switch (errno) {
    case EDOM:
        err_info = "domain error";
        break;
    case EILSEQ:
        err_info = "illegal sequence";
        break;
    case ERANGE:
        err_info = "pole or range error";
        break;
    case 0:
        err_info = "no error";
    }
    fputs(err_info, stdout);
    puts(" occurred");
}
 
int main(void)
{
    fputs("MATH_ERRNO is ", stdout);
    puts(math_errhandling &amp; MATH_ERRNO ? "set" : "not set");
 
    errno = 0;
    1.0/0.0;
    show_errno();
 
    errno = 0;
    acos(+1.1);
    show_errno();
 
    errno = 0;
    log(0.0);
    show_errno();
 
    errno = 0;
    sin(0.0);
    show_errno();
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">MATH_ERRNO is set
no error occurred
domain error occurred
pole or range error occurred
no error occurred</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.5 Errors &lt;errno.h&gt; (p: 205) </li>
<li> K.3.1.3 Use of errno (p: 584) </li>
<li> K.3.2 Errors &lt;errno.h&gt; (p: 585) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.5 Errors &lt;errno.h&gt; (p: 186) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.1.3 Errors &lt;errno.h&gt; </li></ul>
</ul>           <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="errno_macros" title="c/error/errno macros"> <span class="t-lines"><span>E2BIG, EACCES, ..., EXDEV</span></span></a></div> </td> <td> macros for standard POSIX-compatible error conditions <br> <span class="t-mark">(macro constant)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../io/perror" title="c/io/perror"> <span class="t-lines"><span>perror</span></span></a></div> </td> <td> displays a character string corresponding of the current error to <code><a href="../io/std_streams" title="c/io/std streams">stderr</a></code> <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../string/byte/strerror" title="c/string/byte/strerror"> <span class="t-lines"><span>strerror</span><span>strerror_s</span><span>strerrorlen_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> returns a text version of a given error code <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../numeric/math/math_errhandling" title="c/numeric/math/math errhandling"> <span class="t-lines"><span>math_errhandling</span><span>MATH_ERRNO</span><span>MATH_ERREXCEPT</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> defines the error handling mechanism used by the common mathematical functions <br> <span class="t-mark">(macro constant)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/error/errno" title="cpp/error/errno">C++ documentation</a></span> for <code>errno</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/error/errno" class="_attribution-link">https://en.cppreference.com/w/c/error/errno</a>
  </p>
</div>