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()用法的更多相关文章

  1. python assert用法

    使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单.在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行最崩溃,不如在出现错误条件 ...

  2. assert用法

    assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: void assert( int expression ); assert的作用 ...

  3. assert用法总结

    assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:#include <assert.h>void assert( int ...

  4. Delphi 中ASSERT用法

    http://blog.csdn.net/dongyonggan/article/details/5780979 用法:ASSERT(表达式) 如果为假,ASSERT会产生一个EASSERTIONFA ...

  5. spring assert 用法

    spring在提供一个强大的应用开发框架的同时也提供了很多优秀的开发工具类,合理的运用这些工具,将有助于提高开发效率.增强代码质量.下面就最常用的Assert工具类,简要介绍一下它的用法.Assert ...

  6. Delphi 中 断言 Assert 用法

    procedure Assert(expr : Boolean [; const msg: string]); 用法:   Assert(表达式,[显示信息]); 如果为假, assert会产生一个E ...

  7. python学习笔记 - assert用法

    [转自]http://blog.sina.com.cn/s/blog_76e94d210100vz37.html   1.assert语句用来声明某个条件是真的. 2.如果你非常确信某个你使用的列表中 ...

  8. thymeleaf中的th:assert用法

    th:assert 断言标签 th:assert属性可以指定一个以逗号分隔的表达式对其进行评估并生产适用于每一个评价,如果不抛出异常 <div th:assert="${onevar} ...

  9. Junit的Assert用法

    package junit.framework; /** * A set of assert methods. Messages are only displayed when an assert f ...

随机推荐

  1. Qt Creator needs a compiler set up to build. Configure a compiler in the kit options - Stack Overflow

    Qt Creator needs a compiler set up to build. Configure a compiler in the kit options - Stack Overflo ...

  2. SQL server 数据库视频总结

    用了半个多月的时间把,浙江大学耿建玲老师 数据库视频看了一遍.在看视频之前,曾经接收了一个学生信息管理系统,在学习  学生信息管理系统的时候,对于数据库的部分,总是那么一知半解.带着疑惑来看耿建玲老师 ...

  3. mysql待整理

    1. MYSQL SQL_NO_CACHE的真正含义 http://www.dewen.org/q/5149/Mysql 是 结果不缓存,但查询还是缓存了. 如果要重新测试,就在查询前先执行一下&qu ...

  4. nvarchar and nchar

    Same: 1.store unicode charactor. 2. A charactor is stroed with 2 bytes. Different. 1. nchar  会自动填充数据 ...

  5. Quiz 6a Question 7————An Introduction to Interactive Programming in Python

     First, complete the following class definition: class BankAccount: def __init__(self, initial_bal ...

  6. C、C++中“*”操作符和“后++”操作符的优先级

    假设有如下的定义 char carr[] = {"test"}; char cp = carr; 那么表达式 *cp++; 的右值是什么呢? 这个表达式在数组遍历的程序中非常常见, ...

  7. 分支-15. 日K蜡烛图(15)

    #include<iostream> using namespace std; int main(){ float o,h,l,c; while(cin>>o>>h ...

  8. 梳排序(Comb sort)

    Comb Sort,梳排序或者梳子排序,就像梳子那样有间隔地比较两个数,很形象,O(n*logn)时间复杂度,O(1)空间复杂度,属于不稳定的排序算法.算法的思想是使逆序的元素尽可能快地移动到最终的位 ...

  9. 55. 略谈Lotus Notes的与众不同及系列文章至此的总结

    在二十多年的悠久历史里,Lotus Notes发展出一整套独特的概念.技术和思维.由于它早期惊人的领先时代和后续发展中同样惊人的忠于传统,这位软件领域的寿星在如今发展更新速度远超往日和技术愈趋公开互通 ...

  10. Java学习之IO之File类一

    File的操作 package com.gh.file; import java.io.File; import java.io.IOException; /** * File操作 * @author ...