码迷,mamicode.com
首页 > 其他好文 > 详细

断言与静态断言

时间:2020-02-22 19:59:41      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:c99   his   mac   地方   cas   error   hand   exce   导致   

ASSERT:


NAME // 准确陈述 assert - abort the program if assertion is false // assert(断言) - 如果断言(assertion)是错误的就终止这个程序。 SYNOPSIS // 概要 #include <assert.h> // assert函数包含在<assert.h> 这个头文件中 void assert(scalar expression); // 函数原型 :void assert(常量 表达式) DESCRIPTION // 描述 This macro(宏指令) can help programmers find bugs in their programs, or handle exceptional cases via(经由) a crash(失败) that will produce limited debugging output. If expression is false (i.e., compares equal to zero), assert() prints an error message to standard error and terminates(终止) the program by calling abort(3). The error message includes the name of the file and function containing the assert() call, the source code line number of the call, and the text of the argument(理由); something like(有点像): prog: some_file.c:16: some_func: Assertion `val == 0‘ failed. If the macro NDEBUG is defined at the moment <assert.h> was last included, the macro assert() generates(生成) no code, and hence(执行) does nothing at all. It is not recommended(建议) to define NDEBUG if using assert() to detect(检查) error conditions since the software may behave non-deter‐ ministically.

试着翻译一下:(翻译错误的地方请指正)

描述:

这个assert()这个宏指令帮助程序员找程序中的bugs。或者处理经由debug输出限制导致失败的cases。

如果表达式失败,assert()这个宏将打印一个标准错误的信息并且调用abort(3)这个函数来终止这个程序,

这个错误信息包括包含调用asser()宏的函数和包含调用assert()宏的文件名,行号,和理由,有点像:

some_file.c:16: some_func: Assertion `val == 0‘ failed.

如果宏NDEBUG 是定义在<assert.h>之前,则assert()这个宏不生成代码,什么也不执行。
我们不建议在使用essert()进行error检查时定义define。

断言与静态断言

标签:c99   his   mac   地方   cas   error   hand   exce   导致   

原文地址:https://www.cnblogs.com/tito/p/12346736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!