<!DOCTYPE html>
<html class="client-js"><head>
  <meta charset="UTF-8">
  <title>assert.h</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script>
    function importScript() { return 1 } // this is to avoid the error from site.js
  </script>
  <script>let articleId = 'Assert.h'</script>
  <link rel="canonical" href="https://en.wikipedia.org/wiki/Assert.h">
  <link href="../-/mw/ext.cite.ux-enhancements.css" rel="stylesheet" type="text/css">
<link href="../-/mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="../-/mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="../-/mw/skins.minerva.base.reset|skins.minerva.content.styles|ext.cite.style|site.styles|mobile.app.pagestyles.android|mediawiki.page.gallery.styles|mediawiki.skinning.content.parsoid.css" rel="stylesheet" type="text/css">
  <link href="../-/style.css" rel="stylesheet" type="text/css"><link href="../-/content.parsoid.css" rel="stylesheet" type="text/css"><link href="../-/inserted_style.css" rel="stylesheet" type="text/css">
  <script src="../-/script.js"></script><script src="../-/masonry.min.js"></script><script src="../-/article_list_home.js"></script><script src="../-/images_loaded.min.js"></script><script src="../-/node_module/details-element-polyfill/dist/details-element-polyfill.js"></script>
</head>

<body class="mediawiki mw-hide-empty-elt ns-0 ns-subject stable skin-minerva action-view animations">
  <div id="mw-mf-viewport" class="feature-header-v2">
    <div id="mw-mf-page-center">
      <div id="content" class="mw-body">
        <a id="top"></a>
        <div id="bodyContent" class="content mw-parser-output">
          
          <div id="mw-content-text" style="direction: ltr;"><h1 class="section-heading" tabindex="0" aria-haspopup="true" data-section-id="0">
  <span class="mw-headline" id="title_0">assert.h</span>
</h1>
<div id="mf-section-0" class="mf-section-0" aria-pressed="true" aria-expanded="true">
  <p><b>assert.h</b> is a <a href="Header_file" title="Header file" class="mw-redirect">header file</a> in the <a href="C_standard_library" title="C standard library">standard library</a> of the <a href="C_(programming_language)" title="C (programming language)">C programming language</a> that defines the <a href="C_preprocessor" title="C preprocessor">C preprocessor</a> <a href="Macro_(computer_science)" title="Macro (computer science)">macro</a> <code>assert()</code>.<span class="mw-ref reference" id="cite_ref-C99_1-0"><a href="#cite_note-C99-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></span>
<span class="mw-ref reference" id="cite_ref-2"><a href="#cite_note-2" style="counter-reset: mw-Ref 2;"><span class="mw-reflink-text">[2]</span></a></span> In C++ it is also available through the <code><span>&lt;</span>cassert<span>&gt;</span></code> header file.</p>


<p><span>
</span><meta property="mw:PageProp/displaytitle" content="assert.h"></p>
<style data-mw-deduplicate="TemplateStyles:r1003042402">.mw-parser-output .sidebar{width:22em;float:right;clear:right;margin:0.5em 0 1em 1em;background:#f8f9fa;border:1px solid #aaa;padding:0.2em;border-spacing:0.4em 0;text-align:center;line-height:1.4em;font-size:88%}.mw-parser-output .sidebar a{white-space:nowrap}.mw-parser-output .sidebar-wraplinks a{white-space:normal}.mw-parser-output .sidebar-subgroup{width:100%;margin:0;border-spacing:0}.mw-parser-output .sidebar-left{float:left;clear:left;margin:0.5em 1em 1em 0}.mw-parser-output .sidebar-none{float:none;clear:both;margin:0.5em 1em 1em 0}.mw-parser-output .sidebar-outer-title{padding-bottom:0.2em;font-size:125%;line-height:1.2em;font-weight:bold}.mw-parser-output .sidebar-top-image{padding:0.4em 0}.mw-parser-output .sidebar-top-caption,.mw-parser-output .sidebar-pretitle-with-top-image,.mw-parser-output .sidebar-caption{padding-top:0.2em;line-height:1.2em}.mw-parser-output .sidebar-pretitle{padding-top:0.4em;line-height:1.2em}.mw-parser-output .sidebar-title,.mw-parser-output .sidebar-title-with-pretitle{padding:0.2em 0.4em;font-size:145%;line-height:1.2em}.mw-parser-output .sidebar-title-with-pretitle{padding-top:0}.mw-parser-output .sidebar-image{padding:0.2em 0 0.4em}.mw-parser-output .sidebar-heading{padding:0.1em}.mw-parser-output .sidebar-content{padding:0 0.1em 0.4em}.mw-parser-output .sidebar-content-with-subgroup{padding:0.1em 0 0.2em}.mw-parser-output .sidebar-above,.mw-parser-output .sidebar-below{padding:0.3em 0.4em;font-weight:bold}.mw-parser-output .sidebar-collapse .sidebar-above,.mw-parser-output .sidebar-collapse .sidebar-below{border-top:1px solid #aaa;border-bottom:1px solid #aaa}.mw-parser-output .sidebar-navbar{text-align:right;font-size:115%}.mw-parser-output .sidebar-collapse .sidebar-navbar{padding-top:0.6em}.mw-parser-output .sidebar-collapse .mw-collapsible-toggle{margin-top:0.2em}.mw-parser-output .sidebar-list-title{text-align:left;font-weight:bold;line-height:1.6em;font-size:105%}@media(max-width:720px){body.mediawiki .mw-parser-output .sidebar{width:100%!important;clear:both;float:none;margin-left:0!important;margin-right:0!important}}</style><table class="sidebar vertical-navbox nomobile"><tbody><tr><th class="sidebar-title"><a href="C_standard_library" title="C standard library">C standard library</a></th></tr><tr><th class="sidebar-heading">
General topics</th></tr><tr><td class="sidebar-content" style="text-align: left">
<ul><li><a href="C_data_types" title="C data types">Data types</a></li>
<li><a href="C_character_classification" title="C character classification">Character classification</a></li>
<li><a href="C_string_handling" title="C string handling">Strings</a></li>
<li><a href="C_mathematical_functions" title="C mathematical functions">Mathematics</a></li>
<li><a href="C_file_input/output" title="C file input/output">File input/output</a></li>
<li><a href="C_date_and_time_functions" title="C date and time functions">Date/time</a></li>
<li><a href="C_localization_functions" title="C localization functions">Localization</a></li>
<li><a href="C_dynamic_memory_allocation" title="C dynamic memory allocation">Memory allocation</a></li>
<li><a href="C_process_control" title="C process control">Process control</a></li>
<li><a href="C_signal_handling" title="C signal handling">Signals</a></li>
<li><a href="C_alternative_tokens" title="C alternative tokens">Alternative tokens</a></li></ul></td>
</tr><tr><th class="sidebar-heading">
Miscellaneous headers</th></tr><tr><td class="sidebar-content" style="text-align: left">
<ul><li><style data-mw-deduplicate="TemplateStyles:r886049734">.mw-parser-output .monospaced{font-family:monospace,monospace}</style><span class="monospaced">&lt;<a href="Assert.h" title="Assert.h">assert.h</a>&gt;</span></li>
<li><span class="monospaced">&lt;<a href="Errno.h" title="Errno.h">errno.h</a>&gt;</span></li>
<li><span class="monospaced">&lt;<a href="Setjmp.h" title="Setjmp.h">setjmp.h</a>&gt;</span></li>
<li><span class="monospaced">&lt;<a href="Stdarg.h" title="Stdarg.h">stdarg.h</a>&gt;</span></li></ul></td>
</tr><tr><td class="sidebar-navbar"><style data-mw-deduplicate="TemplateStyles:r992953826">.mw-parser-output .navbar{display:inline;font-size:88%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}.mw-parser-output .infobox .navbar{font-size:100%}.mw-parser-output .navbox .navbar{display:block;font-size:100%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}</style></td></tr></tbody></table>


</div><details data-level="2" open="">
    <summary class="section-heading"><h2 id="Assert">Assert</h2></summary>
    

<pre>assert(a<span>&nbsp;</span>!= 1);</pre>

<p>This is a macro that implements a runtime <a href="Assertion_(computing)" title="Assertion (computing)" class="mw-redirect">assertion</a>, which can be used to verify assumptions made by the program and print a diagnostic message if this assumption is false.</p>

<p>When executed, if the expression is false (that is, compares equal to 0), <code>assert()</code> will write information about the call that failed on <code><a href="Standard_streams#Standard_error_(stderr)" title="Standard streams">stderr</a></code> and then call <code><a href="Abort_(computing)" title="Abort (computing)">abort()</a></code>.  The information it writes to <code>stderr</code> includes:</p>
<ul><li>the source filename (the predefined macro <code>__FILE__</code>)</li>
<li>the source line number (the predefined macro <code>__LINE__</code>)</li>
<li>the source function (the predefined identifier <code>__func__</code>) (added in <a href="C99" title="C99">C99</a>)</li>
<li>the text of expression that evaluated to 0 <span class="mw-ref reference" id="cite_ref-C99_1-1"><a href="#cite_note-C99-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></span></li></ul>

<p>Example output of a program compiled on Linux:</p>

<pre>program: program.c:5: main: Assertion `a<span>&nbsp;</span>!= 1' failed.
Abort (core dumped)</pre>

<p>Programmers can eliminate the assertions just by recompiling the program, without changing the source code: if the macro <code>NDEBUG</code> is defined before the inclusion of <code>&lt;assert.h&gt;</code>, the <code>assert()</code> macro may be defined simply as:</p>

<pre>#define assert(ignore)((void) 0)</pre>

<p>and therefore has no effect on the compilation unit, not even evaluating its argument. Therefore expressions passed to <code>assert()</code> must <i>not</i> contain <a href="Side_effect_(computer_science)" title="Side effect (computer science)">side-effects</a> since they will not happen when debugging is disabled. For instance:</p>

<pre>assert(x = gets());</pre>

<p>will not read a line and not assign to x when debugging is disabled.</p>


    <details data-level="3" open="">
    <summary class="section-heading"><h3 id="Additional_message">Additional message</h3></summary>
    

<p>There is no standardized variant of <code>assert()</code> that includes an error message. This can nevertheless be achieved using a <a href="Comma_operator" title="Comma operator">comma operator</a>, which discards all preceding values and only keep the last one:</p>

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="n">assert</span><span class="p">((</span><span class="s">"Orwellian"</span><span class="p">,</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">5</span><span class="p">));</span>
</pre></div>

<p>Will yield something similar to:</p>

<pre>program: program.c:5: main: Assertion `("Orwellian", 2 + 2 == 5)' failed.
Abort</pre>

<p>A more convenient syntax can be made with a macro, here using the name Microsoft uses<span class="mw-ref reference" id="cite_ref-3"><a href="#cite_note-3" style="counter-reset: mw-Ref 3;"><span class="mw-reflink-text">[3]</span></a></span> for a similar macro:</p>

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="cp">#define _ASSERT_EXPR(test, message) assert(((void)(message), test))</span>
<span class="n">_ASSERT_EXPR</span><span class="p">(</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">5</span><span class="p">,</span> <span class="s">"Orwellian"</span><span class="p">);</span>
</pre></div>


</details>

</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="Static_assert">Static assert</h2></summary>
    

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">static_assert</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">20</span><span class="p">,</span> <span class="s">"I need huge integers"</span><span class="p">);</span>
</pre></div>

<p><a href="C%2B%2B11" title="C++11">C++11</a> added a similar keyword <code>static_assert</code><span class="mw-ref reference" id="cite_ref-cppref_4-0"><a href="#cite_note-cppref-4" style="counter-reset: mw-Ref 4;"><span class="mw-reflink-text">[4]</span></a></span> that contextually converts a constant expression to <code>bool</code> and prints a message (optional since C++17<span class="mw-ref reference" id="cite_ref-cppref_4-1"><a href="#cite_note-cppref-4" style="counter-reset: mw-Ref 4;"><span class="mw-reflink-text">[4]</span></a></span>) at compile-time if it is false. It is possible to simulate this using a macro and templates, though this is probably not necessary since most modern C++ compilers support this C++11 feature.</p>

<p>This feature was formally added in <a href="C11_(C_standard_revision)" title="C11 (C standard revision)">C11</a> as the keyword <code>_Static_assert</code> with an identical usage, and in <code>&lt;assert.h&gt;</code> a convenience macro <code>static_assert</code> is added.</p>

<p>It is possible to simulate a static assertion in older versions of C using a macro: <code>#define static_assert(cond, str) char _temp[-!((void)str, (cond))]</code>, though the resulting error is cryptic. (It triggers an error because C allows zero-length arrays, but not negative-length ones.)  <a href="Gnulib" title="Gnulib">Gnulib</a>'s version of the static assert uses sizeof and a structure to trigger a similar error.<span class="mw-ref reference" id="cite_ref-5"><a href="#cite_note-5" style="counter-reset: mw-Ref 5;"><span class="mw-reflink-text">[5]</span></a></span></p>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="Example">Example</h2></summary>
    
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span>
<span class="cp">#include</span> <span class="cpf">&lt;assert.h&gt;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;=</span><span class="mi">9</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">assert</span><span class="p">(</span><span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">4</span><span class="p">);</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">"i = %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
 
<pre>i = 0
i = 1
i = 2
i = 3
i = 4
assert: example.c:10: main: Assertion `i &lt;= 4' failed.
Aborted</pre>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="External_links">External links</h2></summary>
    
<ul><li><span class="neverexpand"><code><a href="https://www.opengroup.org/onlinepubs/9699919799/basedefs/assert.h.html" class="external text external">assert.h</a></code></span><span>:</span><span>&nbsp;</span><span>verify program assertion</span><span>&nbsp;</span><span>–</span><span>&nbsp;</span><span>Base Definitions Reference, </span><a href="Single_Unix_Specification" title="Single Unix Specification" class="mw-redirect">The Single UNIX Specification</a><span>, Issue 7 from </span><a href="The_Open_Group" title="The Open Group">The Open Group</a></li></ul>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="References">References</h2></summary>
    
<div class="reflist " style=" list-style-type: decimal;">
<div class="mw-references-wrap"><ol class="mw-references references"><li id="cite_note-C99-1"> <span id="mw-reference-text-cite_note-C99-1" class="mw-reference-text">International Standard for Programming Language C (C99), ISO/IEC 9899:1999, p. 169</span></li><li id="cite_note-2"> <span id="mw-reference-text-cite_note-2" class="mw-reference-text"><cite class="citation book cs1"><a href="https://web.archive.org/web/20120630234442/http://c-p-p.net/c/assert.h/assert" class="external text external"><i><span>[</span>Coding Programmer Page C / C++ Reference<span>]</span></i></a>. Archived from <a href="http://c-p-p.net/c/assert.h/assert" class="external text external">the original</a> on 2012-06-30<span class="reference-accessdate">. Retrieved <span class="nowrap">2012-03-23</span></span>.</cite></span></li><li id="cite_note-3"> <span id="mw-reference-text-cite_note-3" class="mw-reference-text"><cite class="citation web cs1"><a href="https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/assert-asserte-assert-expr-macros?view=msvc-160" class="external text external">"_ASSERT, _ASSERTE, _ASSERT_EXPR Macros"</a>.</cite></span></li><li id="cite_note-cppref-4"> <span id="mw-reference-text-cite_note-cppref-4" class="mw-reference-text"><a href="https://en.cppreference.com/w/cpp/language/static_assert" class="external free external">https://en.cppreference.com/w/cpp/language/static_assert</a></span></li><li id="cite_note-5"> <span id="mw-reference-text-cite_note-5" class="mw-reference-text"><cite class="citation web cs1"><a href="https://github.com/coreutils/gnulib/blob/master/lib/verify.h" class="external text external">"gnulib/lib/verify.h"</a>. coreutils. 24 November 2019.</cite></span></li></ol></div></div>

<style data-mw-deduplicate="TemplateStyles:r999302996">.mw-parser-output cite.citation{font-style:inherit}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .id-lock-free a,.mw-parser-output .citation .cs1-lock-free a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/6/65/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited a,.mw-parser-output .id-lock-registration a,.mw-parser-output .citation .cs1-lock-limited a,.mw-parser-output .citation .cs1-lock-registration a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/d/d6/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription a,.mw-parser-output .citation .cs1-lock-subscription a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/a/aa/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-ws-icon a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/4/4c/Wikisource-logo.svg")right 0.1em center/12px no-repeat}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-maint{display:none;color:#33aa33;margin-left:0.3em}.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}</style>
    
</details><!--htdig_noindex--><div><div style="clear:both; background-image:linear-gradient(180deg, #E8E8E8, white); border-top: dashed 2px #AAAAAA; padding: 0.5em 0.5em 0.5em 0.5em; margin-top: 1em; direction: ltr;">
    This article is issued from <a class="external text" title="Last edited on 2021-01-20" href="https://en.wikipedia.org/wiki/?title=Assert.h&amp;oldid=1001671679">Wikipedia</a>. The text is licensed under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons - Attribution - Sharealike</a>. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
        </div>
      </div>
    </div>
  </div>
  <script src="../-/mw/jsConfigVars.js"></script>
  <script src="../-/mw/startup.js"></script>
<script src="../-/mw/jquery.js"></script>
<script src="../-/mw/mediawiki.js"></script>
<script src="../-/mw/site.js"></script>
<script src="../-/mw/ext.cite.ux-enhancements.js"></script>



</body></html>