?
Ce document utilise Manuel du site Web PHP chinois Libérer
在頭文件<assert.h>中定義 | ||
---|---|---|
#ifdef NDEBUG #define assert(condition)((void)0)#else #define assert(condition)/ * implementation defined * / #endif |
宏的定義assert
取決于另一個宏NDEBUG
,它不是由標準庫定義的。
如果NDEBUG
在源代碼中<assert.h>
包含的位置被定義為宏名稱,則不assert
執(zhí)行任何操作。
如果NDEBUG
未定義,則assert
檢查其參數(shù)(必須具有標量類型)是否等于零。如果確實如此,則assert
在標準錯誤輸出和調(diào)用上輸出實現(xiàn)特定的診斷信息abort()
。診斷信息需要包括的文本expression
,以及標準的宏的值__FILE__
,__LINE__
和預定義的變量__func__
。(自C99以來)。
條件 | - | 標量類型的表達 |
---|
(none).
#include <stdio.h>// uncomment to disable assert()// #define NDEBUG#include <assert.h>#include <math.h> int main(void){ double x = -1.0; assert(x >= 0.0); printf("sqrt(x) = %f\n", sqrt(x)); return 0;}
輸出:
output with NDEBUG not defined:a.out: main.cpp:10: main: Assertion `x >= 0.0' failed. output with NDEBUG defined:sqrt(x) = -nan
C11標準(ISO/IEC 9899:2011):
7.2.1.1斷言宏(p:186-187)
C99標準(ISO / IEC 9899:1999):
7.2.1.1斷言宏(p:169)
C89 / C90標準(ISO/IEC 9899:1990):
4.2.1.1斷言宏
abort | 導致程序異常終止(不清除)(功能) |
---|
| C++ documentation for assert |