详解C语言中的错误报告errno与其相关应用方法

2025-05-27 0 91

C语言标准库中的错误报告用法有三种形式。
1、errno
errno在<errno.h>头文件中定义,如下

?

1

2

3
#ifndef errno

extern int errno;

#endif

外部变量errno保存库程序中实现定义的错误码,通常被定义为errno.h中以E开头的宏,
所有错误码都是正整数,如下例子

?

1
# define EDOM 33 /* Math argument out of domain of function. */

EDOM的意思是参数不在数学函数能接受的域中,稍后的例子中用到了这个宏。
errno的常见用法是在调用库函数之前先清零,随后再进行检查。

在linux中使用c语言编程时,errno是个很有用的动动。他可以把最后一次调用c的方法的错误代码保留。但是如果最后一次成功的调用c的方法,errno不会改变。因此,只有在c语言函数返回值异常时,再检测errno
errno会返回一个数字,每个数字代表一个错误类型。详细的可以查看头文件。/usr/include/asm/errno.h
如何把errno的数字转换成相应的文字说明?

一个简单的例子

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
#include <stdio.h>

#include <errno.h>

#include <string.h>

#include <math.h>

int main(void)

{

errno = 0;

int s = sqrt(-1);

if (errno) {

printf("errno = %d\\n", errno); // errno = 33

perror("sqrt failed"); // sqrt failed: Numerical argument out of domain

printf("error: %s\\n", strerror(errno)); // error: Numerical argument out of domain

}

return 0;

2、strerror
strerror在<string.h>中定义,如下
__BEGIN_NAMESPACE_STD
/* Return a string describing the meaning of the `errno' code in ERRNUM. */
extern char *strerror (int __errnum) __THROW;
__END_NAMESPACE_STD
函数strerror返回一个错误消息字符串的指针,其内容是由实现定义的,字符串不能修改,但可以在后续调用strerror函数是覆盖。

?

1
char *strerror(int errno)

使用方式如下:

?

1
fprintf(stderr,"error in CreateProcess %s, Process ID %d ",strerror(errno),processID)

将错误代码转换为字符串错误信息,可以将该字符串和其它的信息组合输出到用户界面。
注:假设processID是一个已经获取了的整形ID

3、perror
perror在<stdio.h>中定义,如下
__BEGIN_NAMESPACE_STD
/* Print a message describing the meaning of the value of errno.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void perror (const char *__s);
__END_NAMESPACE_STD
函数perror在标准错误输出流中打印下面的序列:参数字符串s、冒号、空格、包含errno中当前错误码的错误短消息和换行符。在标准C语言中,如果s是NULL指针或NULL字符的指针,则只打印错误短消息,而不打印前面的参数字符串s、冒号及空格。

?

1
void perror(const char *s)

函数说明
perror ( )用来将上一个函数发生错误的原因输出到标准错误(stderr),参数s 所指的字符串会先打印出,后面再加上错误原因 字符串。此错误原因依照全局变量 errno 的值来决定要输出的字符串。
另外并不是所有的c函数调用发生的错误信息都会修改errno。例如gethostbyname函数。
errno是否是线程安全的?
errno是支持线程安全的,而且,一般而言,编译器会自动保证errno的安全性。
我们看下相关头文件 /usr/include/bits/errno.h
会看到如下内容:

?

1

2

3

4

5

6
# if !defined _LIBC || defined _LIBC_REENTRANT

/* When using threads, errno is a per-thread value. */

# define errno (*__errno_location ())

# endif

# endif /* !__ASSEMBLER__ */

#endif /* _ERRNO_H */

也就是说,在没有定义__LIBC或者定义_LIBC_REENTRANT的时候,errno是多线程/进程安全的。
为了检测一下你编译器是否定义上述变量,不妨使用下面一个简单程序。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
#include <stdio.h>

#include <errno.h>

int main( void )

{

#ifndef __ASSEMBLER__

printf( "Undefine __ASSEMBLER__/n" );

#else

printf( "define __ASSEMBLER__/n" );

#endif

#ifndef __LIBC

printf( "Undefine __LIBC/n" );

#else

printf( "define __LIBC/n" );

#endif

#ifndef _LIBC_REENTRANT

printf( "Undefine _LIBC_REENTRANT/n" );

#else

printf( "define _LIBC_REENTRANT/n" );

#endif

return 0;

}

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解C语言中的错误报告errno与其相关应用方法 https://www.kuaiidc.com/75730.html

相关文章

发表评论
暂无评论