- #include
- void assert(scalar expression);
assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。
NAME
assert - abort the program if assertion is false
DESCRIPTION
If the macro NDEBUG was defined at the momentwas last included, the macro assert() generates no code, and hence does nothing at all. Otherwise, the macro assert() prints an error mes‐
sage to standard error and terminates the program by calling abort(3) if expression is false (i.e., compares equal to zero).The purpose of this macro is to help programmers find bugs in their programs. The message "assertion failed in file foo.c, function do_bar(), line 1287" is of no help at all to a user.
如果宏NDEBUG是在
最后被包含的时候定义的,宏assert()不会生成任何代码,因此什么也不做。否则,宏assert()将向标准错误输出错误消息,如果表达式为false(即比较等于零),则调用abort(3)终止程序。这个宏的目的是帮助程序员发现程序中的bug。消息“断言失败在文件foo.c,函数do_bar(),第1287行”对用户毫无帮助 RETURN VALUE
No value is returned.
代码:
- #include
- #include
-
- #define DEBUG_INFO(format,...) printf(\
- "line=%d:"format"\n",__LINE__,\
- ##__VA_ARGS__)
-
- int main(int argc,char *argv)
- {
- int i = 0,j = 1;
- DEBUG_INFO("i = 0");
- assert(i == 0);
- DEBUG_INFO("j = 1");
- assert(j == 0);
- printf("bye\n");
- return 0;
- }
运行结果:从输出结果可知,i初始值是0,assert(i == 0)正常执行,i的值赋值为1,第二个assert(i == 0)就报错了。报错后,assert内部自动调用abort,退出程序。
- csdn@ubuntu:~$ ./a.out
- line=11:i = 0
- line=13:j = 1
- a.out: assert.c:14: main: Assertion `j == 0' failed.
- 已放弃 (核心已转储)
- csdn@ubuntu:~$
验证默认环境是否定义了NDEBUG:修改代码如下:
- #include
- #include
-
-
- #define DEBUG_INFO(format,...) printf(\
- "line=%d:"format"\n",__LINE__,\
- ##__VA_ARGS__)
-
- int main(int argc,char *argv)
- {
- int i = 0,j = 1;
- #ifdef NDEBUG
- DEBUG_INFO("NDEBUG is defined");
- #else
- DEBUG_INFO("NDEBUG is not defined");
- #endif
- DEBUG_INFO("i = 0");
- assert(i == 0);
- DEBUG_INFO("j = 1");
- assert(j == 0);
- printf("bye\n");
- return 0;
- }
运行结果:又结果可知,NDEBUG默认未被定义
- csdn@ubuntu:~$ ./a.out
- line=15:NDEBUG is not defined
- line=17:i = 0
- line=19:j = 1
- a.out: assert.c:20: main: Assertion `j == 0' failed.
- 已放弃 (核心已转储)
- csdn@ubuntu:~$
自定义的NDEBUG必须在#include
- #define NDEBUG
- #include
- #include
-
-
- #define DEBUG_INFO(format,...) printf(\
- "line=%d:"format"\n",__LINE__,\
- ##__VA_ARGS__)
-
- int main(int argc,char *argv)
- {
- int i = 0,j = 1;
- #ifdef NDEBUG
- DEBUG_INFO("NDEBUG is defined");
- #else
- DEBUG_INFO("NDEBUG is not defined");
- #endif
- DEBUG_INFO("i = 0");
- assert(i == 0);
- DEBUG_INFO("j = 1");
- assert(j == 0);
- printf("bye\n");
- return 0;
- }
执行结果:定义了NDEBUG后,就不会再退出程序了。
- csdn@ubuntu:~$ ./a.out
- line=13:NDEBUG is defined
- line=17:i = 0
- line=19:j = 1
- bye
- csdn@ubuntu:~$
-
修改代码如下所示:
- //#define NDEBUG
- #include
- #include
-
- #define DEBUG_INFO(format,...) printf(\
- "line=%d:"format"\n",__LINE__,\
- ##__VA_ARGS__)
- #define DEBUG
- int main(int argc,char *argv)
- {
- int i = 0,j = 1;
- #ifdef NDEBUG
- DEBUG_INFO("NDEBUG is defined");
- #else
- DEBUG_INFO("NDEBUG is not defined");
- #endif
- DEBUG_INFO("i = 0");
- assert(i == 0);
- DEBUG_INFO("j = 1");
- assert(i == 0 || j == 0);
- assert(i == 0 && j == 0);
- printf("bye\n");
- return 0;
- }
运行结果:这说明assert函数也是可以判断多个条件的。
- csdn@ubuntu:~$ ./a.out
- line=15:NDEBUG is not defined
- line=17:i = 0
- line=19:j = 1
- a.out: assert.c:21: main: Assertion `i == 0 && j == 0' failed.
- 已放弃 (核心已转储)
- csdn@ubuntu:~$
实践,检验知识。