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/error%2Ferrno.html | |
new repository
Diffstat (limited to 'devdocs/c/error%2Ferrno.html')
| -rw-r--r-- | devdocs/c/error%2Ferrno.html | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/devdocs/c/error%2Ferrno.html b/devdocs/c/error%2Ferrno.html new file mode 100644 index 00000000..687e991b --- /dev/null +++ b/devdocs/c/error%2Ferrno.html @@ -0,0 +1,78 @@ + <h1 id="firstHeading" class="firstHeading">errno</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><errno.h></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><errno.h></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 <stdio.h> +#include <math.h> +#include <errno.h> + +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 & 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 <errno.h> (p: 205) </li> +<li> K.3.1.3 Use of errno (p: 584) </li> +<li> K.3.2 Errors <errno.h> (p: 585) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.5 Errors <errno.h> (p: 186) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.1.3 Errors <errno.h> </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"> + © 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> |
