c中errno数字与文字描述对应表

本文详细介绍了从0到42的一系列错误代码及其对应的描述,帮助读者理解不同错误代码所表示的具体含义,以便在遇到这些问题时能够迅速定位并解决。
0 :desc : No error
   1 :desc : Operation not permitted
   2 :desc : No such file or directory
   3 :desc : No such process
   4 :desc : Interrupted function call
   5 :desc : Input/output error
   6 :desc : No such device or address
   7 :desc : Arg list too long
   8 :desc : Exec format error
   9 :desc : Bad file descriptor
  10 :desc : No child processes
  11 :desc : Resource temporarily unavailable
  12 :desc : Not enough space
  13 :desc : Permission denied
  14 :desc : Bad address
  15 :desc : Unknown error
  16 :desc : Resource device
  17 :desc : File exists
  18 :desc : Improper link
  19 :desc : No such device
  20 :desc : Not a directory
  21 :desc : Is a directory
  22 :desc : Invalid argument
  23 :desc : Too many open files in system
  24 :desc : Too many open files
  25 :desc : Inappropriate I/O control operation
  26 :desc : Unknown error
  27 :desc : File too large
  28 :desc : No space left on device
  29 :desc : Invalid seek
  30 :desc : Read-only file system
  31 :desc : Too many links
  32 :desc : Broken pipe
  33 :desc : Domain error
  34 :desc : Result too large
  35 :desc : Unknown error
  36 :desc : Resource deadlock avoided
  37 :desc : Unknown error
  38 :desc : Filename too long
  39 :desc : No locks available
  40 :desc : Function not implemented
  41 :desc : Directory not empty
  42 :desc : Illegal byte sequence
<think> 嗯,用户问的是C语言中errno的定义。参考站内引用资料来看: 引用[1]提到errno是定义在<errno.h>头文件中的宏,是一个整数变量用于存储错误代码。引用[2]补充说明这是记录系统最后一次错误代码的机制。引用[3]指出errno在标准中定义为int型左值(lvalue),并列举了EDOM、ERANGE等具体错误码的语义。引用[4]则关联了errno错误处理函数的关系。 需要向用户明确几个关键点:errno本质是宏而非普通变量,具体实现通常是线程安全的存储位置(如引用[2]提到的"实际实现可能是宏扩展"),标准要求的行为特征(如引用[3]说的"函数返回错误时设置")。同时要澄清常见误解——虽然引用[1]说它是"整数变量",但C标准实际允许编译器通过宏实现。 在解释EDOM等错误码时,引用[3]的例子很实用,比如sqrt(-1)触发EDOM,可以保留这个具体案例。最后按用户要求自然关联到strerror/perror的使用提示,引用[4]正好提供了这部分内容。 用户连续两次询问errno定义,可能正在调试底层代码,可以补充说明"通过gcc -E查看宏展开"这种实用技巧。另外引用[2]提到"解决莫名其妙的问题",暗示用户可能需要错误排查指导。 </think>根据C语言标准,`errno` 的定义和核心特性如下: 1. **本质定义** `errno` 是一个定义在 `<errno.h>` 头文件中的**宏**,其行为类似于一个可修改的左值(lvalue)。标准规定: > “`errno` 展开为一个可修改的左值,其类型为 `int`”[^1][^3]。 这意味着在代码中可以直接对其赋值(如 `errno = 0;`)或读取值。 2. **底层实现方式** * **传统实现**:早期可能是全局整型变量。 * **现代实现**:为支持多线程,通常被实现为**线程局部存储(TLS)的宏**。例如在Glibc中,它可能展开为 `(*__errno_location())`,确保每个线程有独立的 `errno` 副本[^2][^4]。 3. **核心作用** 当标准库函数或系统调用发生错误时,**自动设置 `errno` 为特定错误码**,用于标识错误类型。例如: - `EDOM`:参数无效(如 `sqrt(-1)`)[^3] - `ERANGE`:结果超出范围(如 `strtol("9999999999999999999", NULL, 10)`)[^3] - `EILSEQ`:非法字节序列(如字符编码转换错误)[^3] 4. **使用规范** * **检查时机**:仅在函数**明确返回错误值**(如返回 `-1` 或 `NULL`)后检查 `errno`。 * **重置**:函数调用前可手动置零:`errno = 0;` * **错误信息转换**: - `strerror(errno)`:将错误码转为字符串描述 - `perror("提示信息")`:直接打印错误描述(格式:`提示信息: 错误描述`)[^4] ### 示例代码 ```c #include <stdio.h> #include <errno.h> #include <math.h> #include <string.h> int main() { errno = 0; // 重置错误码 double result = sqrt(-1); // 触发错误 if (errno != 0) { printf("错误码: %d\n", errno); // 输出错误码(如 EDOM=33) printf("错误描述: %s\n", strerror(errno)); // 输出可读描述 perror("sqrt函数失败"); // 输出: sqrt函数失败: Numerical argument out of domain } return 0; } ``` ### 关键总结 | 特性 | 说明 | |--------------|----------------------------------------------------------------------| | **标准头文件** | `<errno.h>` | | **类型** | 展开为 `int` 类型的可修改左值(宏) | | **线程安全** | 现代实现通过TLS保证多线程安全 | | **错误关联** | 函数返回错误时**自动设置**,成功时不保证重置 | | **错误处理** | 需搭配 `strerror()` 或 `perror()` 获取可读描述 [^4] |
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值