assert()用法
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:[1]
#include <assert.h>
void assert( int expression );
assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。
//demo1.c
#include <stdio.h>
#include <assert.h>
#include <stdlib.h> int demo1(int num)
{
assert(num>); printf("demo2:%d\n",num);
return ;
}
//demo2.c
#include <stdio.h>
#include <assert.h>
#include <stdlib.h> int demo2(int num)
{
assert(num>); printf("demo2:%d\n",num);
return ;
}
//demo.c
#include <stdio.h>
#include <stdlib.h> int main( void )
{
demo1();
demo2();//不满足 num>200的条件,报错如下
return ;
}
测试现象:
root@ubuntu:/work/demo# gcc *.c ;./a.out
demo2:
a.out: demo2.c:: demo2: Assertion `num>' failed.
Aborted
在调试结束后,可以通过在包含#include <assert.h>的语句之前插入 #define NDEBUG 来禁用assert调用,示例代码如下:
#include <stdio.h>
#define NDEBUG
#include <assert.h>
如:
//demo2.c
#include <stdio.h>
#define NDEBUG //仅对本文件起作用
#include <assert.h>
#include <stdlib.h> int demo2(int num)
{
assert(num>);//未编译展开宏。 printf("demo2:%d\n",num);
return ;
}
现象:
root@ubuntu:/work/demo# gcc *.c ;./a.out
demo2:
demo2:
参考:
1. assert()函数用法总结
http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html
assert()用法的更多相关文章
- python assert用法
使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单.在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行最崩溃,不如在出现错误条件 ...
- assert用法
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: void assert( int expression ); assert的作用 ...
- assert用法总结
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:#include <assert.h>void assert( int ...
- Delphi 中ASSERT用法
http://blog.csdn.net/dongyonggan/article/details/5780979 用法:ASSERT(表达式) 如果为假,ASSERT会产生一个EASSERTIONFA ...
- spring assert 用法
spring在提供一个强大的应用开发框架的同时也提供了很多优秀的开发工具类,合理的运用这些工具,将有助于提高开发效率.增强代码质量.下面就最常用的Assert工具类,简要介绍一下它的用法.Assert ...
- Delphi 中 断言 Assert 用法
procedure Assert(expr : Boolean [; const msg: string]); 用法: Assert(表达式,[显示信息]); 如果为假, assert会产生一个E ...
- python学习笔记 - assert用法
[转自]http://blog.sina.com.cn/s/blog_76e94d210100vz37.html 1.assert语句用来声明某个条件是真的. 2.如果你非常确信某个你使用的列表中 ...
- thymeleaf中的th:assert用法
th:assert 断言标签 th:assert属性可以指定一个以逗号分隔的表达式对其进行评估并生产适用于每一个评价,如果不抛出异常 <div th:assert="${onevar} ...
- Junit的Assert用法
package junit.framework; /** * A set of assert methods. Messages are only displayed when an assert f ...
随机推荐
- jQuery EasyUI 数字框(NumberBox)用法
这里的options是选项,可以参考下表: 选项名 类型 描述 默认值 min 数字 文本框中可允许的最小值 null max 数字 文本框中可允许的最大值 null precision 数字 最高可 ...
- Android手机设置隐藏命令大全
注意:因Android版本较多,固有部分隐藏命令或不能使用 *#*#4636#*#* 显示手机信息.电池信息.电池记录.使用统计数据.WiFi 信息 *#*#7780#*#* 重设为原厂设定,不会删除 ...
- lightoj Again Array Queries
1100 - Again Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...
- codeigniter IE浏览器下无法登录的解决的方法
站点搬迁到新的server后,CI 框架做的站点IE浏览器下无法登录.登录时候採用CI自带的SESSION机制,事实上是以COOKIE保存. 网上搜索到IE浏览器不支持域名存在- _. 不是这个原因, ...
- wpf全局异常
在App.xaml文件中 添加DispatcherUnhandledExceptionEventArgs 新增对应事件
- SharePoint将网站另存为模板
1.将网站另存为模板 参考文章 http://blog.csdn.net/dyp330/article/details/23180843 http://blog.163.com/berlin1989@ ...
- MVC+ADO模式
MVC+DAO设计模式 博客分类: Java Java WEB开发 MVC+DAO设计模式 本文摘自:http://www.paper.edu.cn 基于MVC+DAO设计模式的Struts ...
- 转:C++:从子类访问父类的私有函数
众所周知,c和c++的数组都是不安全的,因为无论c还是c++都不提供数组边界检查功能,这使得数组溢出成为可能.从某个意义上说,c和c++是一种缺少监督的语言,然而这也正是其魅力所在.c++给予程序员更 ...
- python测试框架--nose
最近再浏览Python自动化测试框架,之前接触过unittest,看了篇文章,发现nose貌似更牛逼一些,于是安装试了试,分享一下心得. nose 项目是于 2005 年发布的,也就是 p ...
- BZOJ 1677: [Usaco2005 Jan]Sumsets 求和
题目 1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 617 Solved: 344[Su ...