#include <assert.h>
assert.h is one of the required,
standard (ISO9899 -
ANSI C)
headers and is used for
debugging C programs. It provides the pseudo-function (actually a
macro)
void assert(int).
When called, if the argument evaluates to false (or 0) then the assertion has failed and the macro is invoked.
- To STDERR:
- Expression (the expression that failed)
- __FILE__ (file name)
- __LINE__ (line number)
Program aborts.
For example (this file is named assert.c):
#include <assert.h>
int main(void)
{
assert(1 == 0);
}
After preprocessing (gcc -E assert.c):
int main(void)
{
((void) (( 1 == 0 ) ? 0 : (__assert_fail ("1 == 0", "assert.c", 5, __PRETTY_FUNCTION__ ), 0))) ;
}
Produces the output:
% ./assert
assert: assert.c:5: main: Assertion `1 == 0' failed.
Abort
%
While it is good form to use asserts during debugging and testing, it is considered a very bad thing for an end user to see an assert failure (even a segmentation fault is preferable to some debug code left in in many cases). In these cases, the use of 'NDEBUG' is recommended. Having this #define present will change assert's macro to be a simple void. This can be done on many C compliers by specifying -DNDEBUG on the command line.