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

断言assert主要用于程序开发中检测逻辑错误,当表达式为假时终止程序。调试完成后,通过定义NDEBUG可关闭assert。本文分析了assert的实现机制,包括宏定义和_wassert函数,并给出使用示例,强调了断言在检查参数合法性、避免条件替换等方面的作用。
摘要由CSDN通过智能技术生成

断言(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&#

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值