C语言头文件 assert.h

一、assert.h

C 标准库的 assert.h头文件提供了一个名为 assert 的宏,它可用于验证程序做出的假设,并在假设为假时输出诊断消息。
void assert(int expression) 这实际上是一个宏,不是一个函数,可用于在 C 程序中添加诊断。

$ cat 1.c
#include <assert.h>
void main() {
assert(1 < 0);
}
$ ./a.out
a.out: 1.c:3: main: Assertion `1 < 0' failed.
Aborted

在引用assert.h 之前, 定义NDEBUG 为空,则 assert 无效

$ cat 1.c 
#define NDEBUG
#include <assert.h>
void main() {
    assert(1 < 0);
}
$ ./a.out 
$ 

 

posted @ 2024-07-25 09:34  靖意风  Views(108)  Comments(0)    收藏  举报