断言assert原型
void assert(int expression);
assert宏的原型定义在<assert.h>中,其作用是先计算表达式expression的值为假(即为0),那么它就先向stderr打印一条出错信息,然后通过条用abort来终止程序;
使用assert的缺点是,频繁的调用会极大的影响程序的性能,增加额外的开销。
在调试结束后,可以通过在包含#include 的语句之前插入 #define NDEBUG 来禁用assert调用,示例代码如下:
1 #include
2 #define NDEBUG
3 #include
断言assert使用规则
#include <stdio.h>
#include <assert.h>
int main(void)
{
int i;
i = 1;
assert(i++);
printf(“%d\n”,i);
return 0;
}
看运行结果,如果给定的i初始值为1,所以其运行结果不会为错,如下图所示