assert.h is the header file of the standard library of the C programming language , in which the macro of the C preprocessor assert() declared. This macro implements an exception that can be used to check the calculations made by the program.
| Standard library C programming language |
|
Macros
The assert() macro adds a diagnostic routine to the program. After execution, if the expression is false (that is, the comparison result is 0), assert() writes the call information to the stderr stream and calls the abort() function. The information that is written in stderr includes:
- source file name (predefined macro
__FILE__) - a line in the source file (predefined macro
__LINE__) - function in the source code (predefined macro
__func__) (added in the C99 standard) - text of an expression whose value is zero 0
In order to disable the check, it is not necessary to exclude it from the code or comment on the macro declaration, you just need to declare another macro - NDEBUG in the program before #include <assert.h> :
#define NDEBUG
then the macro declaration assert() will look like this:
#define assert (ignore) ((void) 0)
and therefore will not affect the operation of the program.
The assert () macro is overridden each time assert.h connected, depending on the NDEBUG macro. [one]
The assert() macro is implemented as a macro, not a function. If the assert macro is used to call the assert function itself, then the correct functioning of the code is not guaranteed.
Usage Example
#include <stdio.h>
#include <assert.h>
int main ( void )
{
FILE * fd ;
fd = fopen ( "/home/user/file.txt" , "r" );
assert ( fd );
fclose ( fd );
return 0 ;
}
In this example, the assert() macro will fire if the fopen() function fails.
See also
- Approval (programming)
Links
assert.h- Basic Definitions, The Single UNIX® Specification , Issue 7 by The Open Group