c语言断言函数,Assert(断言实现机制深入剖析)

断言(assert)的作用是用来判断程序运行的正确性,确保程序运行的行为与我们理解的一致。其调用形式为assert(logic expression),如果逻辑表达式为假,则调用abort()终止程序的运行。

查看MSDN帮助文档,可以得到assert的解释信息如下:

The ANSI assert macro is typically used to identify logic errors during program development, by implementing the expression argument to evaluate to false only when the program is operating incorrectly. After debugging is complete, assertion checking can be turned off without modifying the source file by defining the identifier NDEBUG. NDEBUG can be defined with a /D command-line option or with a #define directive. If NDEBUG is defined with #define, the directive must appear before ASSERT.H is included.

翻译过来大概意思就是assert是通过判断其参数的真假来标识程序的逻辑错误,调试结束后可以通过定义NDEBUG来关闭assert断言。

查看include/assert.h头文件可以得到assert相关的宏写义如下:

#ifdef  NDEBUG

#define assert(exp)     ((void)0)

#else

#ifdef  __cplusplus

extern "C&#

你可能感兴趣的:(c语言断言函数)