?
本文檔使用 PHP中文網(wǎng)手冊 發(fā)布
在頭文件<errno.h>中定義 | ||
---|---|---|
#define errno / *實現(xiàn)定義的* / |
errno
是一個預(yù)處理器宏,可擴展為線程本地(自C11)可修改類型的左值int
。幾個標準庫函數(shù)通過寫入正整數(shù)來指示錯誤errno
。通常,值的errno
設(shè)置為<errno.h>
以字母E
開頭的后綴為大寫字母或數(shù)字的宏常量中列出的錯誤代碼之一。
值errno
是0
在程序啟動時,盡管庫函數(shù)可以寫為正整數(shù)errno
是否發(fā)生了錯誤,庫函數(shù)永遠保存0
在errno
。
庫函數(shù)perror
,strerror
可用于獲取與當前errno
值對應(yīng)的錯誤條件的文本描述。
#include <stdio.h>#include <math.h>#include <errno.h> void show_errno(void){ if(errno==EDOM) printf("domain error"); if(errno==EILSEQ) printf("illegal sequence"); if(errno==ERANGE) printf("pole or range error"); if(errno==0) printf("no error"); printf(" occurred\n");} int main(void){ printf("MATH_ERRNO is %s\n", math_errhandling & MATH_ERRNO ? "set" : "not set"); errno = 0; 1.0/0.0; show_errno(); errno = 0; acos(+1.1); show_errno(); errno = 0; log(0.0); show_errno(); errno = 0; sin(0.0); show_errno();}
輸出:
MATH_ERRNO is setpole or range error occurred domain error occurred pole or range error occurred no error occurred
C11標準(ISO/IEC 9899:2011):
7.5錯誤<errno.h>(p:205)
K.3.1.3使用errno(p:584)
K.3.2錯誤<errno.h>(p:585)
C99標準(ISO/IEC 9899:1999):
7.5錯誤<errno.h>(p:186)
C89 / C90標準(ISO/IEC 9899:1990):
4.1.3錯誤<errno.h>
E2BIG,EACCES,...,EXDEV | 用于標準POSIX兼容錯誤條件的宏(宏常量) |
---|---|
PERROR | 顯示當前錯誤對應(yīng)的字符串到stderr(函數(shù)) |
strerrorstrerror_sstrerrorlen_s(C11)(C11) | 返回給定錯誤代碼(函數(shù))的文本版本 |
math_errhandlingMATH_ERRNOMATH_ERREXCEPT(C99)(C99)(C99) | 定義了常用數(shù)學函數(shù)(宏常量)使用的錯誤處理機制, |