assert
来自cppreference.com
|
|
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
| 在头文件 <assert.h> 中定义
|
||
| #ifdef NDEBUG #define assert(condition) ((void)0) |
||
依赖于另一个宏,
assert,这是没有定义的标准库中定义的宏NDEBUG.原文:
The definition of the macro
assert depends on another macro, NDEBUG, which is not defined by the standard library.NDEBUG是
<assert.h>是包括在源代码中的点定义为宏的名称,然后assert一无所有原文:
If NDEBUG is defined as a macro name at the point in the source code where
<assert.h> is included, then assert does nothing.NDEBUG没有定义,然后
assert检查,如果其参数(必须有标量类型)比较等于零。如果是的话,assert输出实现特定的标准错误输出的诊断信息,并调用abort()。需要的诊断信息,包括文本expression,以及价值观的标准宏__FILE__,__LINE__和标准的变量__func__.原文:
If NDEBUG is not defined, then
assert checks if its argument (which must have scalar type) compares equal to zero. If it does, assert outputs implementation-specific diagnostic information on the standard error output and calls abort(). The diagnostic information is required to include the text of expression, as well as the values of the standard macros __FILE__, __LINE__, and the standard variable __func__.目录 |
[编辑] 参数
| condition | - | 标量类型的表达
|
[编辑] 返回值
(无)
[编辑] 示例
#include <stdio.h> #include <assert.h> int main (int argc, char **argv) { // Test if 0 is really equivalent to 0 assert (0 == 0); // Test if 1 is different than 0... assert (1 == 0); return 0; }
输出:
example: ex.c:10: int main(int, char**): Assertion `1 == 0' failed. Aborted
[编辑] 另请参阅
| 引发非正常的程序终止(不清理) (函数) | |