曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 3721|回复: 0
打印 上一主题 下一主题

assert

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2009-5-28 22:12:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

#include <assert.h>

void assert( int exp );
功能: assert()用于错误检测。如果表达式的结果为零,宏写错误信息到STDERR并退出程序执行。如果宏NDEBUG已经定义,宏assert()将被忽略。

测试代码
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>

int main()
{
    int fd;

    fd = open ("test.txt", O_RDONLY);
   
    assert (fd > 0);

    printf ("test.txt can be open\n");

    return (0);
}
说明
这里已经事先知道 test.txt 在当前目录下不存在,所以无法打开,fd 为 -1。在 assert() 函数里,它的要求是 fd > 0 程序才能往下走,否则出错退出,而且还会提示出错的行数。中文 linux 环境下运行此程序运行输出为:
./assert
assert: assert.c:13: main: Assertion `fd > 0' failed.
已放弃

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-6 07:57 , Processed in 0.076562 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表