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%2Fassert.html | |
new repository
Diffstat (limited to 'devdocs/c/error%2Fassert.html')
| -rw-r--r-- | devdocs/c/error%2Fassert.html | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/devdocs/c/error%2Fassert.html b/devdocs/c/error%2Fassert.html new file mode 100644 index 00000000..d194a1b0 --- /dev/null +++ b/devdocs/c/error%2Fassert.html @@ -0,0 +1,48 @@ + <h1 id="firstHeading" class="firstHeading">assert</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><assert.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">#ifdef NDEBUG +#define assert(condition) ((void)0) +#else +#define assert(condition) /*implementation defined*/ +#endif</pre> +</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>The definition of the macro <code>assert</code> depends on another macro, <code>NDEBUG</code>, which is not defined by the standard library.</p> +<p>If <code>NDEBUG</code> is defined as a macro name at the point in the source code where <code><assert.h></code> is included, then <code>assert</code> does nothing.</p> +<p>If <code>NDEBUG</code> is not defined, then <code>assert</code> checks if its argument (which must have scalar type) compares equal to zero. If it does, <code>assert</code> outputs implementation-specific diagnostic information on the standard error output and calls <code><a href="http://en.cppreference.com/w/c/program/abort"><span class="kw486">abort</span></a><span class="br0">(</span><span class="br0">)</span></code>. The diagnostic information is required to include the text of <code>expression</code>, as well as the values of <span class="t-rev-inl t-since-c99"><span> the <a href="../language/function_definition" title="c/language/function definition">predefined variable</a> <code>__func__</code> and of</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span> the <a href="../preprocessor/replace" title="c/preprocessor/replace">predefined macros</a> <code>__FILE__</code> and <code>__LINE__</code>.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> condition </td> <td> - </td> <td> expression of scalar type </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>(none)</p> +<h3 id="Notes"> Notes</h3> <p>There is no standardized interface to add an additional message to <code>assert</code> errors. A portable way to include one is to use a <a href="../language/operator_other#Comma_operator" title="c/language/operator other">comma operator</a>:</p> +<div class="c source-c"><pre data-language="c">assert(("There are five lights", 2 + 2 == 5));</pre></div> <p>The implementation of <code>assert</code> in <a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/assert-macro-assert-wassert?view=msvc-160">Microsoft CRT</a> does not conform to C99 and later revisions, because its underlying function (<code>_wassert</code>) takes neither <code>__func__</code> nor an equivalent replacement.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +// uncomment to disable assert() +// #define NDEBUG +#include <assert.h> +#include <math.h> + +int main(void) +{ + double x = -1.0; + assert(x >= 0.0); + printf("sqrt(x) = %f\n", sqrt(x)); + + return 0; +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">output with NDEBUG not defined: +a.out: main.cpp:10: main: Assertion `x >= 0.0' failed. + +output with NDEBUG defined: +sqrt(x) = -nan</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.2.1.1 The assert macro (p: 135) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.2.1.1 The assert macro (p: 186-187) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.2.1.1 The assert macro (p: 169) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.2.1.1 The assert macro </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../program/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 colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/error/assert" title="cpp/error/assert">C++ documentation</a></span> for <code>assert</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/assert" class="_attribution-link">https://en.cppreference.com/w/c/error/assert</a> + </p> +</div> |
