From WikiChip
Difference between revisions of "c/assert.h/assert"
(Created page with "{{DISPLAYTITLE: assert macro - <assert.h> - C}} The '''assert''' macro can be used to insert diagnostic tests into a program.") |
m |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{c title|assert macro - <assert.h>}}{{Assert.h - C}} |
− | The '''assert''' macro can be used to insert diagnostic tests into a program. | + | The '''assert''' macro, which is part of {{C|assert.h|<assert.h>}} can be used to insert diagnostic tests into a program. |
+ | |||
+ | == Synopsis == | ||
+ | <source lang="c">#include <assert.h> | ||
+ | void assert(scalar expression);</source> | ||
+ | |||
+ | == Description == | ||
+ | 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 ({{C|__FILE__}}) | ||
+ | * The source line number ({{C|__LINE__}}) | ||
+ | * The name of the enclosing function ({{C|__func__}}) ([[C99]]) | ||
+ | |||
+ | 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> | ||
+ | |||
+ | == Parameters == | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Parameter !! Description | ||
+ | |- | ||
+ | | expression || The expression to be evaluated | ||
+ | |} | ||
+ | |||
+ | == Returns == | ||
+ | The assert macro returns no value. | ||
+ | |||
+ | == Example == | ||
+ | <source lang="c">#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; | ||
+ | }</source> | ||
+ | |||
+ | Example Output: | ||
+ | |||
+ | <pre>i = 0 | ||
+ | i = 1 | ||
+ | i = 2 | ||
+ | i = 3 | ||
+ | i = 4 | ||
+ | i = 5 | ||
+ | example: example.c:11: main: Assertion `i < 5' failed. | ||
+ | Aborted (core dumped)</pre> | ||
+ | |||
+ | Starting at variable i being set to zero, the loop continues until the assertion fails when i reaches 5. The program terminates by calling [[Stdlib.h/abort|abort()]] after printing the diagnostic message. | ||
+ | |||
+ | == See also == | ||
+ | * {{C|Assert.h/static_assert|static_assert macro}} | ||
+ | * {{C|assert.h|<assert.h> header}} | ||
+ | * {{C|standard library|C Standard Library}} | ||
+ | * [[C]] | ||
+ | |||
+ | {{DEFAULTSORT:assert}} | ||
+ | [[Category:assert.h - 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.