?
? ????? PHP ??? ???? ??? ?? ??
在頭文件<assert.h>中定義 | ||
---|---|---|
#ifdef NDEBUG #define assert(condition)((void)0)#else #define assert(condition)/ * implementation defined * / #endif |
宏的定義assert
取決于另一個宏NDEBUG
,它不是由標(biāo)準(zhǔn)庫定義的。
如果NDEBUG
在源代碼中<assert.h>
包含的位置被定義為宏名稱,則不assert
執(zhí)行任何操作。
如果NDEBUG
未定義,則assert
檢查其參數(shù)(必須具有標(biāo)量類型)是否等于零。如果確實(shí)如此,則assert
在標(biāo)準(zhǔn)錯誤輸出和調(diào)用上輸出實(shí)現(xiàn)特定的診斷信息abort()
。診斷信息需要包括的文本expression
,以及標(biāo)準(zhǔn)的宏的值__FILE__
,__LINE__
和預(yù)定義的變量__func__
。(自C99以來)。
條件 | - | 標(biāo)量類型的表達(dá) |
---|
(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標(biāo)準(zhǔn)(ISO/IEC 9899:2011):
7.2.1.1斷言宏(p:186-187)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.2.1.1斷言宏(p:169)
C89 / C90標(biāo)準(zhǔn)(ISO/IEC 9899:1990):
4.2.1.1斷言宏
abort | 導(dǎo)致程序異常終止(不清除)(功能) |
---|
| C++ documentation for assert |