C标准库之三:errno.h

本文介绍了C标准库中的errno.h头文件,详细解释了errno、EDOM和ERANGE宏的作用及使用方法。通过设置和检查errno,开发者可以有效诊断C库函数的运行错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:概述

      errno.h是C标准库中的一个检查错误的头文件,以我个人的理解,它的内部一共定义了3个宏,分别是EDOM,ERANGE,errno.其中errno相当于一个全局变量,它可以被赋值,而C库中的每个模块都会包含这个errno.h头文件,那么errno这个可以被赋值的宏(不知道这样理解对不对,看了C标准库才做的理解),就相当于整个C标准库的全局变量,如果在使用某个模块的函数过程中发生了错误,这个errno宏就会在函数内部被赋予一个有含义的值,相当于一个错误代码,并且无论库的哪个模块或库函数都不会把errno设置为零。所以我们在使用这个模块的时候,只需要在一个库函数调用前将errno设置为零,然后在调用之后检查errno的值即可知道这个被调用的库函数有没有发生错误。
      而另外两个宏则被赋予为固定的错误类型,就像平常的宏一样表示一种特定的错误。(以上纯属个人看过C标准库之后的个人理解)

二:模块接口和解释

序号解释
1EDOM特定含义的错误,在math.h 中表示域错误
2ERANGE特定含义的错误,在math.h 中表示溢出错误
3errno库函数中用来盛放错误代码的宏

三:常用接口用法举例

     这几个宏的用法已经在概述中说了,就是在使用库函数时候,在使用之前把errno置为零,然后在使用之后就去检测,其返回值,是不是被库函数赋予了错误代码,当然我们既然包含了errno.h这个头文件当然就可以定义自己的错误代码,也就是我们自己定义的函数内部也可以给errno赋值,然后在使用的时候去判断,前提是错误代码我们需要自己定义,每一个有固定的含义,这样就很方便了。
In file included from cryptlib.c:117:0: cryptlib.h:62:21: fatal error: stdlib.h: No such file or directory # include <stdlib.h> ^ compilation terminated. mem.c:59:19: fatal error: stdio.h: No such file or directory #include <stdio.h> ^ compilation terminated. mem_clr.c:60:20: fatal error: string.h: No such file or directory #include <string.h> ^ compilation terminated. mem_dbg.c:112:19: fatal error: stdio.h: No such file or directory #include <stdio.h> ^ compilation terminated. In file included from cversion.c:59:0: cryptlib.h:62:21: fatal error: stdlib.h: No such file or directory # include <stdlib.h> ^ compilation terminated. In file included from ex_data.c:141:0: cryptlib.h:62:21: fatal error: stdlib.h: No such file or directory # include <stdlib.h> ^ compilation terminated. cpt_err.c:62:19: fatal error: stdio.h: No such file or directory #include <stdio.h> ^ compilation terminated. In file included from uid.c:56:0: ../include/openssl/crypto.h:120:21: fatal error: stdlib.h: No such file or directory # include <stdlib.h> ^ compilation terminated. o_time.c:65:20: fatal error: string.h: No such file or directory #include <string.h> ^ compilation terminated. o_str.c:60:19: fatal error: ctype.h: No such file or directory #include <ctype.h> ^ compilation terminated. o_dir.c:60:19: fatal error: errno.h: No such file or directory #include <errno.h> ^ compilation terminated. In file included from o_fips.c:59:0: cryptlib.h:62:21: fatal error: stdlib.h: No such file or directory # include <stdlib.h> ^ compilation terminated. In file included from o_init.c:56:0: ../e_os.h:468:28: fatal error: unistd.h: No such file or directory # include OPENSSL_UNISTD ^ compilation terminated. getenv.c:14:20: fatal error: stdlib.h: No such file or directory #include <stdlib.h> ^ compilation terminated. Makefile:138: recipe for target 'local_depend' failed make[1]: *** [local_depend] Error 1 make[1]: Leaving directory '/home/holand/work/openssl-OpenSSL_1_0_2u/crypto' Makefile:483: recipe for target 'depend' failed make: *** [depend] Error 1
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值