is one of the required, standard
(ISO9899 - ANSI C
and is used for debug
ging 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)
For example (this file is named assert.c):
assert(1 == 0);
After preprocessing (gcc -E assert.c):
((void) (( 1 == 0 ) ? 0 : (__assert_fail ("1 == 0", "assert.c", 5, __PRETTY_FUNCTION__ ), 0))) ;
Produces the output:
assert: assert.c:5: main: Assertion `1 == 0' failed.
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.