C语言中assert的使用

assert宏的原型为

1 #include <assert.h>
2 void assert(int expression);

作用为计算expression,若其值为假(0),先向stderr打印一条出错信息,后调用abort来终止程序运行!

一般调试阶段使用assert,调试结束后可以通过在#include<assert.h>的语句之前插入#define NDEBUG来禁用assert调用

#include<stdio.h>
#define NDEBUG
#include <assert.h>

注意:

1,因为assert一般在调试阶段使用,调试结束后就会禁用,所以assert不能使用改变环境的语句,如

assert(++i<100);

2,assert和后面的语句最好空一行,以形成逻辑和视觉上的一致感!

posted @ 2015-12-27 19:48  史昊  阅读(638)  评论(0编辑  收藏  举报