C Standard Library 简明教程

C Library - <assert.h>

标准 C 库的 assert.h 头文件提供了一个称为 assert 的宏,该宏可用于验证程序做出的假设,并在该假设为假时打印诊断消息。

The assert.h header file of the C Standard Library provides a macro called assert which can be used to verify assumptions made by the program and print a diagnostic message if this assumption is false.

已定义的宏 assert 指的是另一个宏 NDEBUG ,它不属于 <assert.h> 的一部分。如果在包含 <assert.h> 时在源文件中将 NDEBUG 定义为宏名称,则 assert 宏将按如下方式定义 −

The defined macro assert refers to another macro NDEBUG which is not a part of <assert.h>. If NDEBUG is defined as a macro name in the source file, at the point where <assert.h> is included, the assert macro is defined as follows −

#define assert(ignore) ((void)0)

Library Macros

以下是头 assert.h 中定义的唯一函数 −

Following is the only function defined in the header assert.h −

Sr.No.

Function & Description

1

void assert(int expression)This is actually a macro and not a function, which can be used to add diagnostics in your C program.

2

static_assert(boolean_expression, message)This macro issues a compile-time diagnostic if the value of a constant expression is false.