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

Gretel ·
更新时间:2024-09-20
· 683 次阅读

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

#ifndef errno extern int errno; #endif

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

# 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的数字转换成相应的文字说明?

一个简单的例子

#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函数是覆盖。

char *strerror(int errno)

使用方式如下:

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、冒号及空格。

void perror(const char *s)

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

# 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是多线程/进程安全的。
为了检测一下你编译器是否定义上述变量,不妨使用下面一个简单程序。

#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; }

您可能感兴趣的文章:C语言中返回错误信息的相关函数用法总结C语言创建链表错误之通过指针参数申请动态内存实例分析C语言初学者代码中的常见错误与问题C语言编程时常犯十八个错误小结C语言调试手段:锁定错误的实现方法深入理解C语言中编译相关的常见错误基于C语言中段错误的问题详解详解C语言中scanf函数使用的一些注意点C语言 volatile与const同时使用应注意的问题基础C语言编程时易犯错误有哪些



c语言中 errno 方法 C语言

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章
Genet 2020-03-11
820