<!DOCTYPE html>
<html class="client-nojs vector-feature-language-in-header-enabled vector-feature-language-in-main-menu-disabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-0 vector-toc-not-available vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-0 skin-theme-clientpref-os vector-sticky-header-enabled skin-thumbsize-clientpref-standard" lang="fr" dir="ltr"><head>
    <meta charset="UTF-8">
    <title>Assert.h</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" type="image/png" href="./_res_/favicon.png">
    <link rel="canonical" href="https://fr.wikipedia.org/wiki/Assert.h"> <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_/ext.wikimediamessages.styles.css" rel="stylesheet" type="text/css">
    <link href="./_mw_/skins.vector.icons.css" rel="stylesheet" type="text/css">
    <link href="./_mw_/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
    <link href="./_mw_/skins.vector.styles.css" rel="stylesheet" type="text/css">
    <meta name="ResourceLoaderDynamicStyles" content="">
    
    <link rel="stylesheet" type="text/css" href="./_mw_/site.styles.css">
    <link rel="stylesheet" type="text/css" href="./_mw_/noscript.css">
    <link rel="stylesheet" type="text/css" href="./_res_/footer.css">
    <link rel="stylesheet" type="text/css" href="./_res_/vector-2022.css">
  </head>
  <body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Assert_h rootpage-Assert_h skin-vector-2022 action-view">
    <div class="mw-page-container">
      <div class="mw-page-container-inner">
        <div class="mw-content-container">
          <main id="content" class="mw-body">
            <header class="mw-body-header vector-page-titlebar">
              <h1 id="firstHeading" class="firstHeading mw-first-heading">assert.h</h1>
            </header>
            <a id="top"></a>
            <div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
              <div id="contentSub">
                <div id="mw-content-subtitle"></div>
              </div>
              <div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="fr" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="fr" dir="ltr">
<p>
<code><b>&lt;assert.h&gt;</b></code> est un fichier d'<a href="Header_(informatique)" class="mw-redirect" title="Header (informatique)">en-tête</a> présent dans la <a href="Biblioth%C3%A8que_standard_du_C" title="Bibliothèque standard du C">bibliothèque standard</a> du <a href="C_(langage)" title="C (langage)">langage C</a> qui définit la <a href="Macro-d%C3%A9finition" title="Macro-définition">macro</a> <code>assert</code>. La macro met en œuvre une <a href="Assertion" title="Assertion">assertion</a>, qui peut être utilisée pour vérifier les hypothèses formulées par le programme.
</p><p>Pour le <a href="C%2B%2B" title="C++">langage C++</a>, le fichier équivalent est 
<code><b>&lt;cassert&gt;</b></code><sup id="cite_ref-:0_1-0" class="reference"><a href="#cite_note-:0-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>.
</p><p>L'assertion est la base du <a href="Paradigme_(programmation)" title="Paradigme (programmation)">paradigme</a> de <a href="Programmation_par_contrat" title="Programmation par contrat">programmation par contrat</a><sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>. Elle peut être utilisée pour tester une <a href="Pr%C3%A9condition" title="Précondition">précondition</a>, une postcondition ou un invariant.
</p><p>La macro <code>assert</code> diagnostique les assertions dans le <a href="Programme_informatique" title="Programme informatique">programme</a>. Lorsqu'il est exécuté, si l'expression est vraie, le programme continue. Mais si l'expression est fausse (c'est-à-dire égale à 0), <code>assert</code> écrit des informations sur l'appel qui a échoué dans <code>stderr</code>, puis appelle <code>abort</code>&nbsp;; plus précisément, les informations écrites dans <code>stderr</code> sont&nbsp;:
</p>
<ul><li>le nom du <a href="Code_source" title="Code source">fichier source</a>&nbsp;;</li>
<li>le numéro de la ligne concernée dans le code source&nbsp;;</li>
<li>la fonction mise en jeu dans le code source&nbsp;;</li>
<li>le texte de l'expression qui a été évaluée à 0.</li></ul>
<p>Les assertions implémentées par la macro <code>assert</code> ne sont diagnostiquées que lors de l'exécution du code compilé en mode de débogage. Si le code est compilé en mode opérationnel (dit mode <i>release</i>), les assertions ne sont pas diagnostiquées<sup id="cite_ref-:0_1-1" class="reference"><a href="#cite_note-:0-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>.
</p>
<div class="mw-heading mw-heading2"><h2 id="Exemple_d'utilisation"><span id="Exemple_d.27utilisation"></span>Exemple d'utilisation</h2></div>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;stdio.h&gt;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;assert.h&gt;</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">test_assert</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">)</span>
<span class="p">{</span>
<span class="w">   </span><span class="n">assert</span><span class="p">(</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">);</span>
<span class="w">   </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>

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

<span class="w">    </span><span class="k">for</span><span class="w"> </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="w"> </span><span class="n">i</span><span class="o">&lt;=</span><span class="mi">9</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="w">        </span><span class="n">test_assert</span><span class="p">(</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">);</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">"i = %i</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>Le code précédent affiche le résultat suivant&nbsp;:
</p>
<pre>i = 0
i = 1
i = 2
i = 3
i = 4
assert: assert.c:6: test_assert: Assertion `x &lt;= 4' failed.
Aborted
</pre>
<p>Ici, le programme est interrompu pendant l'exécution de <code>test_assert</code> appelée avec le paramètre 5.
</p>
<div class="mw-heading mw-heading2"><h2 id="Références"><span id="R.C3.A9f.C3.A9rences"></span>Références</h2></div>
<div class="references-small decimal" style=""><div class="mw-references-wrap"><ol class="references">
<li id="cite_note-:0-1"><span class="reference-text"><span class="ouvrage">«&nbsp;<a rel="nofollow" class="external text" href="https://www.programiz.com/cpp-programming/assertions"><cite style="font-style:normal;">C++ Assert</cite></a>&nbsp;», sur <span class="italique">www.programiz.com</span> <small style="line-height:1em;">(consulté le <time class="nowrap" datetime="2025-03-10" data-sort-value="2025-03-10">10 mars 2025</time>)</small></span></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><a href="#cite_ref-2">↑</a> </span><span class="reference-text"><span class="ouvrage" id="CHENU2008"><span class="ouvrage" id="Emmanuel_CHENU2008">Emmanuel CHENU, «&nbsp;<a rel="nofollow" class="external text" href="http://manu40k.free.fr/articleProgrammationParContrat.pdf"><cite style="font-style:normal;">Programmez par Contrat</cite></a>&nbsp;» <span class="skin-invert-image" typeof="mw:File"><span title="Accès libre au document"><img alt="Accès libre" src="./_assets_/0c70a452f799bfe840676ee341124611/Lock-green.svg.png" decoding="async" width="9" height="14" class="mw-file-element" data-file-width="512" data-file-height="813" loading="lazy"></span></span> <abbr class="abbr indicateur-format format-pdf" title="Document au format Portable Document Format (PDF) d'Adobe">[PDF]</abbr>, <time class="nowrap" datetime="2008-10-29" data-sort-value="2008-10-29">29 octobre 2008</time> <small style="line-height:1em;">(consulté le <time class="nowrap" datetime="2025-03-10" data-sort-value="2025-03-10">10 mars 2025</time>)</small></span></span></span>
</li>
</ol></div>
</div>
<ul id="bandeau-portail" class="bandeau-portail"><li><span class="bandeau-portail-element"><span class="bandeau-portail-icone"><span class="noviewer" typeof="mw:File"><img alt="icône décorative" src="./_assets_/0c70a452f799bfe840676ee341124611/Circle-icons-dev.svg.png" decoding="async" width="24" height="24" class="mw-file-element" data-file-width="512" data-file-height="512" loading="lazy"></span></span> <span class="bandeau-portail-texte">Portail de la programmation informatique</span> </span></li>                    </ul></div><!--htdig_noindex--><div><div class="zim-footer">
    Cet article est issu de <a class="external text" title="Dernière modification le 2025-08-19" href="https://fr.wikipedia.org/wiki/?title=Assert.h&amp;oldid=228262124">Wikipédia</a>. Sauf mention contraire, le texte est disponible sous <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.fr">Creative Commons Attribution-Share Alike 4.0</a>. Des conditions supplémentaires peuvent s’appliquer aux fichiers multimédias.
</div>
</div><!--/htdig_noindex--></div>
            </div>
          </main>
        </div>
      </div>
    </div>
     <script src="./_webp_/webpHandler.js"></script>
  

</body></html>