The prototype of an Assert macro is defined in <assert.h>, and its function is to terminate the program execution if its condition returns an error, the prototype definition: [1]
#include <assert.h>
void assert (int expression);
The function of an assert is to evaluate expression expressions, if the value is False (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort.
//demo1.c#include <stdio.h>#include<assert.h>#include<stdlib.h>intDemo1 (intnum) {Assert (num> -); printf ("demo2:%d\n", num); return 0;}//demo2.c#include <stdio.h>#include<assert.h>#include<stdlib.h>intDemo2 (intnum) {Assert (num> $); printf ("demo2:%d\n", num); return 0;}//demo.c#include <stdio.h>#include<stdlib.h>intMainvoid) {demo1 ( the); Demo2 ( the)//Do not meet the num>200 conditions, the error is as followsreturn 0; }
test phenomenon: [email protected]:/work/demo# gcc *.c;. /a . Out Demo2: out: demo2.c:7: demo2:assertion ' num> ' failed. Aborted
After debugging, you can disable the Assert call by inserting the #define NDEBUG before the statement that contains # include <assert.h>, as shown in the following example code:
#include <stdio.h>
#define Ndebug
#include <assert.h>
Such as:
//demo2.c
#include <stdio.h> #define ndebug // #include < Assert.h> #include <stdlib.h> int Demo2 (int num) {assert (num >< Span style= "color: #800080;" >200 demo2:%d\n " ,num); return 0 ;}
phenomenon: [email protected]:/work/demo# gcc *.c;. /a . Out Demo2:demo2:
Reference:
1. Summary of ASSERT () function usage
Http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html
ASSERT () usage