Files
oldlinux-files/study/Ref-docs/C/assert.html
2024-02-19 00:25:23 -05:00

69 lines
2.5 KiB
HTML

<HTML><HEAD><TITLE>&lt;assert.h&gt;</TITLE></HEAD><BODY BGCOLOR="#FFFFFF">
<H1><A NAME="&lt;assert.h&gt;"><CODE>&lt;assert.h&gt;</CODE></A></H1><HR>
<P><CODE>
#undef assert<BR>
#if defined NDEBUG<BR>
#define <B><A HREF="#assert">assert</A></B>(test) (void)0<BR>
#else<BR>
#define <B><A HREF="#assert">assert</A></B>(test)
<I>&lt;void expression&gt;</I><BR>
#endif
</CODE></P>
<P>Include the standard header <B><CODE>&lt;assert.h&gt;</CODE></B>
to define the macro
<A HREF="#assert"><CODE>assert</CODE></A>,
which is useful for diagnosing logic errors in
the program. You can eliminate the testing code produced by the macro
<CODE>assert</CODE> without removing the macro references from the program
by defining the macro
<B><A NAME="NDEBUG"><CODE>NDEBUG</CODE></A></B>
in the program before you include
<CODE>&lt;assert.h&gt;</CODE>. Each time the program includes this header,
it redetermines the definition of the macro <CODE>assert</CODE>.</P>
<H2><A NAME="assert"><CODE>assert</CODE></A></H2>
<P><CODE>
#undef assert<BR>
#if defined NDEBUG<BR>
#define <B>assert</B>(test) (void)0<BR>
#else<BR>
#define <B>assert</B>(test) <I>&lt;void expression&gt;</I><BR>
#endif
</CODE></P>
<P>If the <I>int</I> expression <CODE>test</CODE> equals zero,
the macro writes to
<A HREF="stdio.html#stderr" tppabs="http://ccs.ucsd.edu/c/stdio.html#stderr"><CODE>stderr</CODE></A>
a diagnostic message that includes:</P>
<UL>
<LI>the text of <CODE>test</CODE>
<LI>the source filename (the predefined macro
<A HREF="preproc.html#__FILE__" tppabs="http://ccs.ucsd.edu/c/preproc.html#__FILE__"><CODE>__FILE__</CODE>)</A>
<LI>the source line number (the predefined macro
<A HREF="preproc.html#__LINE__" tppabs="http://ccs.ucsd.edu/c/preproc.html#__LINE__"><CODE>__LINE__</CODE>)</A>
</UL>
<P>It then calls
<A HREF="stdlib.html#abort" tppabs="http://ccs.ucsd.edu/c/stdlib.html#abort"><CODE>abort</CODE></A>.</P>
<P>You can write the macro <CODE>assert</CODE> in the program in any
<A HREF="function.html#side-effects context" tppabs="http://ccs.ucsd.edu/c/function.html#side-effects context">side-effects context</A>.</P>
<HR>
<P>See also the
<B><A HREF="index.html#Table of Contents" tppabs="http://ccs.ucsd.edu/c/index.html#Table of Contents">Table of Contents</A></B> and the
<B><A HREF="_index.html" tppabs="http://ccs.ucsd.edu/c/_index.html">Index</A></B>.</P>
<P><I>
<A HREF="crit_pb.html" tppabs="http://ccs.ucsd.edu/c/crit_pb.html">Copyright</A> &#169; 1989-1996
by P.J. Plauger and Jim Brodie. All rights reserved.</I></P>
</BODY></HTML>