From WikiChip
Difference between revisions of "c/assert.h/assert"
< c‎ | assert.h

m
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE: assert macro - <assert.h> - C}}
+
{{c title|assert macro - <assert.h>}}{{Assert.h - C}}
{{Assert.h - C}}
+
The '''assert''' macro, which is part of {{C|assert.h|<assert.h>}} can be used to insert diagnostic tests into a program.
The '''assert''' macro, which is part of <[[assert.h - C|assert.h]]> can be used to insert diagnostic tests into a program.
 
  
 
== Synopsis ==
 
== Synopsis ==
Line 13: Line 12:
  
 
* The text of the argument
 
* The text of the argument
* The name of the source file ([[__FILE__ - C|__FILE__]])
+
* The name of the source file ({{C|__FILE__}})
* The source line number ([[__LINE__ - C|__LINE__]])
+
* The source line number ({{C|__LINE__}})
* The name of the enclosing function ([[__func__ - C|__func__]]) ([[C99]])
+
* The name of the enclosing function ({{C|__func__}}) ([[C99]])
  
If '''NDEBUG''' is defined as a macro name prior to the inclusion of <[[assert.h - C|assert.h]]>, assert gets defined as:
+
If '''NDEBUG''' is defined as a macro name prior to the inclusion of {{C|assert.h|<assert.h>}}, assert gets defined as:
  
 
<source lang="c">#define assert(ignore) ((void)0)</source>
 
<source lang="c">#define assert(ignore) ((void)0)</source>
Line 63: Line 62:
  
 
== See also ==
 
== See also ==
* [[Assert.h/static_assert - C|static_assert macro]]
+
* {{C|Assert.h/static_assert|static_assert macro}}
* [[assert.h - C|<assert.h> header]]
+
* {{C|assert.h|<assert.h> header}}
* [[C standard library]]
+
* {{C|standard library|C Standard Library}}
* [[C Programming Language]]
+
* [[C]]
  
{{DEFAULTSORT:assert - assert.h - C}}
+
{{DEFAULTSORT:assert}}
 
[[Category:assert.h - C]]
 
[[Category:assert.h - C]]
 
[[Category:Assertion - C]]
 
[[Category:Assertion - C]]

Latest revision as of 07:27, 4 January 2015

The assert macro, which is part of <assert.h> can be used to insert diagnostic tests into a program.

Synopsis[edit]

#include <assert.h>
void assert(scalar expression);

Description[edit]

When executed, if the expression is false (I.E. compares to zero), a diagnostic message is printed in an implementation-defined format to the standard error stream. Assert then calls the abort function.

The diagnostic message includes:

  • The text of the argument
  • The name of the source file (__FILE__)
  • The source line number (__LINE__)
  • The name of the enclosing function (__func__) (C99)

If NDEBUG is defined as a macro name prior to the inclusion of <assert.h>, assert gets defined as:

#define assert(ignore) ((void)0)

Parameters[edit]

Parameter Description
expression The expression to be evaluated

Returns[edit]

The assert macro returns no value.

Example[edit]

#include <assert.h>
#include <stdio.h>

int main(void)
{
    int i;

    for (i = 0; i < 10; i++)
    {
        printf("i = %d\n", i);
        assert(i < 5);
    }

    return 0;
}

Example Output:

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
example: example.c:11: main: Assertion `i < 5' failed.
Aborted (core dumped)

Starting at variable i being set to zero, the loop continues until the assertion fails when i reaches 5. The program terminates by calling abort() after printing the diagnostic message.

See also[edit]