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 ...
随机推荐
- Ext JS学习第六天 Ext自定义类(一)
此文来记录学习笔记 •我们在之前的学习,已经对ExtJS有了一个初步的认识,那么如果要学好ExtJS,对于javascript是必须的,也就是说,对于理解ExtJS底层基础架构的理解也是必须的.那么我 ...
- 在C#中使用 Win32 和其他库
C# 用户经常提出两个问题:“我为什么要另外编写代码来使用内置于 Windows® 中的功能?在框架中为什么没有相应的内容可以为我完成这一任务?”当框架小组构建他们的 .NET 部分时,他们评估了为使 ...
- BCS--使用SharePoint Designer创建外部内容类型
使用SharePoint Designer创建外部列表(也可以在浏览器中创建列表) http://www.cnblogs.com/haogj/archive/2011/05/01/2033845.ht ...
- [译]Stairway to Integration Services Level 13 - SSIS 变量回顾
介绍 在前一篇中我们组合了已经学过的事件冒泡 event bubbling, 日志记录 logging, 和父子模型 Parent-Child pattern 建立了自定义的SSIS包日志记录. 本文 ...
- CAEmitterLayer 粒子发射器
在iOS 5中,苹果引入了一个新的CALayer子类叫做CAEmitterLayer.CAEmitterLayer是一个高性能的粒子引擎,被用来创建实时例子动画如:烟雾,火,雨等等这些效果. CAEm ...
- Mac 终端命令运行java
链接地址:http://www.cnblogs.com/wangrui-techbolg/archive/2012/12/29/2839047.html 由于mac已经装好java环境,所以直接课运行 ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )
按鲜嫩程度排个序, 从大到小处理, 用平衡树维护价值 ---------------------------------------------------------------------- #i ...
- Servlet乘法表学习笔记
一.控制台实现乘法表 package com.shanrengo; import java.io.IOException; import java.io.PrintWriter; import jav ...
- win7和ubuntu双系统,win7时间晚8小时解决办法。
装了Win7和Ubuntu双系统后发现,使用Ubuntu后再登陆win7时系统显示时间不准确,比实际时间晚了8小时. 搜索后发现原来Linux和Windows的系统时间管理是不同的.Linux是以主板 ...
- [转]lftp的致命错误:证书验证:不信任
原文:http://rajaseelan.com/2011/12/18/lftp-fatal-error-certificate-verification-not-trusted/如果您使用lftp的 ...