m (→Diagnostic message) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{c title|<assert.h> Header}}{{C Standard Library}} |
− | {{C Standard Library}} | ||
The '''<assert.h>''' header is part of the [[standard library]] of the [[C programming language]] that provides two macros that implement an assertion - a way to verify that some assumption made by the code is true. If the assumption was false, a diagnostic message gets printed. The header provides both a run-time assertion and a compile-time assertion. | The '''<assert.h>''' header is part of the [[standard library]] of the [[C programming language]] that provides two macros that implement an assertion - a way to verify that some assumption made by the code is true. If the assumption was false, a diagnostic message gets printed. The header provides both a run-time assertion and a compile-time assertion. | ||
Line 6: | Line 5: | ||
== Debug mode == | == Debug mode == | ||
− | The header depends on another macro | + | The header depends on another macro {{C|NDEBUG}} which is not defined by <assert.h> itself. If NDEBUG is defined as a macro name at the point in the source file where <assert.h> is included, the assert macro behave as if they were noops - with absolutely no effect on the program. |
<source lang="c">#define NDEBUG | <source lang="c">#define NDEBUG | ||
Line 19: | Line 18: | ||
! Macro Name !! Description !! Since | ! Macro Name !! Description !! Since | ||
|- | |- | ||
− | | | + | | {{C|Assert.h/assert|assert}} || implements a run-time assertion || [[C89]] |
|- | |- | ||
− | | | + | | {{C|Assert.h/static_assert|static_assert}} || implements a compile-time assertion || [[C11]] |
|} | |} | ||
== Diagnostic message == | == Diagnostic message == | ||
− | An example of the diagnostic message as a result of the | + | An example of the diagnostic message as a result of the {{C|Assert.h/assert|assert}} macro is: |
<pre>Assertion failed: data != NULL, function load_data, file foo.c, line 94.</pre> | <pre>Assertion failed: data != NULL, function load_data, file foo.c, line 94.</pre> | ||
== Run-time assertion == | == Run-time assertion == | ||
− | '''Run-time assertion''' refers to the ability to assert a certain condition during the execution of the program. The | + | '''Run-time assertion''' refers to the ability to assert a certain condition during the execution of the program. The {{C|assert}} macro provides such facilities. Checking the certain conditions are true during the execution of a program is crucial for validating that an algorithm is performing correctly. |
== Compile-time assertion == | == Compile-time assertion == | ||
− | '''Compile-time assertion''' refers to the ability to assert a certain condition during the compilation stage, provided the condition evaluates to a compile-time constant. The | + | '''Compile-time assertion''' refers to the ability to assert a certain condition during the compilation stage, provided the condition evaluates to a compile-time constant. The {{C|Assert.h/static_assert|static_assert}} macro provides the facilities for a compile-time assertion. |
− | {{DEFAULTSORT:Assert.h | + | {{DEFAULTSORT:Assert.h}} |
[[Category:C standard library]] | [[Category:C standard library]] | ||
[[Category:assert.h - C]] | [[Category:assert.h - C]] | ||
[[Category:Assertion - C]] | [[Category:Assertion - C]] |
Latest revision as of 07:22, 4 January 2015
The <assert.h> header is part of the standard library of the C programming language that provides two macros that implement an assertion - a way to verify that some assumption made by the code is true. If the assumption was false, a diagnostic message gets printed. The header provides both a run-time assertion and a compile-time assertion.
The macros evaluates an expression. When the expression is false, I.E. compares to 0, the assert macro writes information about the particular call that failed on to the standard error file.
Debug mode[edit]
The header depends on another macro NDEBUG which is not defined by <assert.h> itself. If NDEBUG is defined as a macro name at the point in the source file where <assert.h> is included, the assert macro behave as if they were noops - with absolutely no effect on the program.
#define NDEBUG
#include <assert.h>
/* assertions have no effect on the program */
Macros[edit]
The header provides two macro definitions:
Macro Name | Description | Since |
---|---|---|
assert | implements a run-time assertion | C89 |
static_assert | implements a compile-time assertion | C11 |
Diagnostic message[edit]
An example of the diagnostic message as a result of the assert macro is:
Assertion failed: data != NULL, function load_data, file foo.c, line 94.
Run-time assertion[edit]
Run-time assertion refers to the ability to assert a certain condition during the execution of the program. The assert macro provides such facilities. Checking the certain conditions are true during the execution of a program is crucial for validating that an algorithm is performing correctly.
Compile-time assertion[edit]
Compile-time assertion refers to the ability to assert a certain condition during the compilation stage, provided the condition evaluates to a compile-time constant. The static_assert macro provides the facilities for a compile-time assertion.