C语言第四篇阅读1-assert
更新时间:2023-10-18 01:32:01 阅读量: 综合文库 文档下载
- c语言实验四推荐度:
- 相关推荐
assert
Evaluates an expression and when the result is FALSE, prints a diagnostic message and aborts the program. void assert( int expression ); Routine assert For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB LIBCMT.LIB MSVCRT.LIB Return Value None Parameter
Single thread static library, retail version Multithread static library, retail version Import library for MSVCRT.DLL, retail version Required Header Compatibility ANSI, Win 95, Win NT expression
Expression (including pointers) that evaluates to nonzero or 0 Remarks
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 prints a diagnostic message when expression evaluates to false (0) and calls abort to terminate program execution. No action is taken if expression is true (nonzero). The diagnostic message includes the failed expression and the name of the source file and line number where the assertion failed.
The destination of the diagnostic message depends on the type of
application that called the routine. Console applications always receive the message via stderr. In a single- or multithreaded Windows application,
assert calls the Windows MessageBox API to create a message box to display the message along with an OK button. When the user chooses OK, the program aborts immediately.
When the application is linked with a debug version of the run-time libraries, assert creates a message box with three buttons: Abort, Retry, and Ignore. If the user selects Abort, the program aborts immediately. If the user selects Retry, the debugger is called and the user can debug the program if Just-In-Time (JIT) debugging is enabled. If the user selects Ignore, assert continues with its normal execution: creating the message box with the OK button. Note that choosing Ignore when an error condition exists can result in “undefined behavior.” For more information, see Using C Run-Time Library Debugging Support.
The assert routine is available in both the release and debug versions of the C run-time libraries. Two other assertion macros, _ASSERT and _ASSERTE, are also available, but they only evaluate the expressiosn passed to them when the _DEBUG flag has been defined. Example
/* ASSERT.C: In this program, the analyze_string function uses * the assert function to test several conditions related to
* string and length. If any of the conditions fails, the program * prints a message indicating what caused the failure. */
#include
void analyze_string( char *string ); /* Prototype */
void main( void ) {
char test1[] = \
printf ( \ analyze_string( test1 );
printf ( \ analyze_string( test2 );
printf ( \ analyze_string( test3 ); }
/* Tests a string to see if it is NULL, */ /* empty, or longer than 0 characters */ void analyze_string( char * string ) {
assert( string != NULL ); /* Cannot be NULL */ assert( *string != '\\0' ); /* Cannot be empty */
assert( strlen( string ) > 2 ); /* Length must exceed 2 */ } Output
Analyzing string 'abc' Analyzing string '(null)'
Assertion failed: string != NULL, file assert.c, line 24
abnormal program termination
Error Handling Routines | Process and Environment Control Routines See Also abort, raise, signal, _ASSERT, _ASSERTE, _DEBUG
正在阅读:
C语言第四篇阅读1-assert10-18
中国男子优秀足球运动员比赛跑动能力研究05-04
后张法预应力空心板施工方案12-24
2020年八年级上册(苏科版)第一章声现象第二节乐音的特性培优训练07-24
H3C VRRP配置案例12-19
《平凡的的世界》孙少安人物形象分析06-20
电力系统分析03-04
2018-2019年高中生物人教版《必修一 分子与细胞》《第2章 组成细胞的分子》第5节 细胞中的10-16
中英合同金额数字的表达10-08
- 必修一物理寒假作业
- 2019-201X年5月大学生入党积极分子思想汇报-word范文模板(3页)
- 药物分析习题五
- 重拾应用意识 体会数学价值(沈建军)
- 2017全国高校辅导员结构化面试题集及参考答案
- 广东徐闻县实验中学2014届高三第二次月测地理试题
- 今天你共鸣了么?
- 2018-2019正能量读后感1000字-推荐word版(6页)
- 2018年中国截切型盖板针布行业专题研究分析报告目录
- 中国移动业务处理流程大全
- 公文写作常用词汇和句子集锦2016
- ARM课程设计说明书
- 教师资格证教育学论文
- 中考试卷分析
- 环境监测试卷(五)
- 党风廉政建设广播稿1
- 快速制作香香宫煮麻辣烫教程
- 《国际金融学》习题
- 文明施工保障措施方案
- 春兰维修资料故障代码
- 语言
- assert
- 阅读