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">Error handling</h1> <h3 id="Error_numbers"> Error numbers</h3> <table class="t-dsc-begin"> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><errno.h></code> </th>
</tr> <tr class="t-dsc"> <td> <div><a href="error/errno" title="c/error/errno"> <span class="t-lines"><span>errno</span></span></a></div> </td> <td> macro which expands to POSIX-compatible thread-local error number variable<br><span class="t-mark">(macro variable)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="error/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> </table> <h3 id="Assertions"> Assertions</h3> <table class="t-dsc-begin"> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><assert.h></code> </th>
</tr> <tr class="t-dsc"> <td> <div><a href="error/assert" title="c/error/assert"> <span class="t-lines"><span>assert</span></span></a></div> </td> <td> aborts the program if the user-specified condition is not <code>true</code>. May be disabled for release builds <br> <span class="t-mark">(function macro)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="error/static_assert" title="c/error/static assert"> <span class="t-lines"><span>static_assert</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span><span class="t-mark-rev t-until-c23">(removed in C23)</span></span></span></div> </td> <td> issues a compile-time diagnostic if the value of a constant expression is false <br> <span class="t-mark">(keyword macro)</span> </td>
</tr> </table> <table class="t-rev-begin"> <tr class="t-rev t-since-c11">
<td> <h3 id="Bounds_checking"> Bounds checking</h3> <p>The standard library provides bounds-checked versions of some existing functions (<code><a href="io/gets" title="c/io/gets">gets_s</a></code>, <code><a href="io/fopen" title="c/io/fopen">fopen_s</a></code>, <code><a href="io/fprintf" title="c/io/fprintf">printf_s</a></code>, <code><a href="string/byte/strcpy" title="c/string/byte/strcpy">strcpy_s</a></code>, <code><a href="string/wide/wcscpy" title="c/string/wide/wcscpy">wcscpy_s</a></code>, <code><a href="string/multibyte/mbstowcs" title="c/string/multibyte/mbstowcs">mbstowcs_s</a></code>, <code><a href="algorithm/qsort" title="c/algorithm/qsort">qsort_s</a></code>, <code><a href="program/getenv" title="c/program/getenv">getenv_s</a></code>, etc). This functionality is <i>optional</i> and is only available if <code>__STDC_LIB_EXT1__</code> is defined. The following macros and functions support this functionality.</p>
<table class="t-dsc-begin"> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><errno.h></code> </th>
</tr> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><stdio.h></code> </th>
</tr> <tr class="t-dsc"> <td> <div><span class="t-lines"><span>errno_t</span></span></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> a typedef for the type <code>int</code>, used to self-document functions that return <code><a href="error/errno" title="c/error/errno">errno</a></code> values <br> <span class="t-mark">(typedef)</span> </td>
</tr> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><stddef.h></code> </th>
</tr> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><stdio.h></code> </th>
</tr> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><stdlib.h></code> </th>
</tr> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><string.h></code> </th>
</tr> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><time.h></code> </th>
</tr> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><wchar.h></code> </th>
</tr> <tr class="t-dsc"> <td> <div><span class="t-lines"><span>rsize_t</span></span></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> a typedef for the same type as <code><a href="types/size_t" title="c/types/size t">size_t</a></code>, used to self-document functions that range-check their parameters at runtime <br> <span class="t-mark">(typedef)</span> </td>
</tr> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><stdint.h></code> </th>
</tr> <tr class="t-dsc"> <td> <div><span class="t-lines"><span>RSIZE_MAX</span></span></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> largest acceptable size for bounds-checked functions, expands to either constant or variable which may change at runtime (e.g. as the currently allocated memory size changes)<br><span class="t-mark">(macro variable)</span> </td>
</tr> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><stdlib.h></code> </th>
</tr> <tr class="t-dsc"> <td> <div><a href="error/set_constraint_handler_s" title="c/error/set constraint handler s"> <span class="t-lines"><span>set_constraint_handler_s</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> set the error callback for bounds-checked functions <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="error/abort_handler_s" title="c/error/abort handler s"> <span class="t-lines"><span>abort_handler_s</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> abort callback for the bounds-checked functions <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="error/ignore_handler_s" title="c/error/ignore handler s"> <span class="t-lines"><span>ignore_handler_s</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> ignore callback for the bounds-checked functions <br> <span class="t-mark">(function)</span> </td>
</tr> </table> <p>Note: implementations of bounds-checked functions are available as open-source libraries <a rel="nofollow" class="external text" href="https://github.com/rurban/safeclib/">Safe C</a> and <a rel="nofollow" class="external text" href="https://code.google.com/archive/p/slibc/">Slibc</a>, and as part of Watcom C. There is also an incompatible set of bounds-checked functions available in Visual Studio.</p>
</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
</tr> </table> <h3 id="Notes"> Notes</h3> <p>Since C23, <a href="language/_static_assert" title="c/language/ Static assert"><code>static_assert</code></a> is itself a keyword, which may also be a predefined macro, so <code><assert.h></code> no longer provides it.</p>
<h3 id="References"> References</h3> <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.2 Diagnostics <assert.h> (p: 186-187) </li>
<li> 7.5 Errors <errno.h> (p: 205) </li>
<li> 7.19 Common definitions <stddef.h> (p: 288) </li>
<li> 7.20 Integer types <stdint.h> (p: 289-295) </li>
<li> 7.21 Input/output <stdio.h> (p: 296-339) </li>
<li> 7.22 General utilities <stdlib.h> (p: 340-360) </li>
<li> K.3.1.3 Use of errno (p: 584) </li>
<li> K.3.2/2 errno_t (p: 585) </li>
<li> K.3.3/2 rsize_t (p: 585) </li>
<li> K.3.4/2 RSIZE_MAX (p: 585) </li>
<li> 7.31.3 Errors <errno.h> (p: 455) </li>
<li> 7.31.10 Integer types <stdint.h> (p: 456) </li>
<li> 7.31.11 Input/output <stdio.h> (p: 456) </li>
<li> 7.31.12 General utilities <stdlib.h> (p: 456) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.2 Diagnostics <assert.h> (p: 169) </li>
<li> 7.5 Errors <errno.h> (p: 186) </li>
<li> 7.26.3 Errors <errno.h> (p: 401) </li>
<li> 7.26.8 Integer types <stdint.h> (p: 401) </li>
<li> 7.26.9 Input/output <stdio.h> (p: 402) </li>
<li> 7.26.10 General utilities <stdlib.h> (p: 402) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul>
<li> 4.2 DIAGNOSTICS <assert.h> </li>
<li> 4.1.3 Errors <errno.h> </li>
<li> 4.13.1 Errors <errno.h> </li>
<li> 4.13.6 Input/output <stdio.h> </li>
<li> 4.13.7 General utilities <stdlib.h> </li>
</ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <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" title="cpp/error">C++ documentation</a></span> for <span class=""><span>Error handling</span></span> </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" class="_attribution-link">https://en.cppreference.com/w/c/error</a>
</p>
</div>
|