?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
在頭文件<errno.h>中定義 | ||
---|---|---|
#define errno / *實(shí)現(xiàn)定義的* / |
errno
是一個(gè)預(yù)處理器宏,可擴(kuò)展為線程本地(自C11)可修改類型的左值int
。幾個(gè)標(biāo)準(zhǔn)庫函數(shù)通過寫入正整數(shù)來指示錯(cuò)誤errno
。通常,值的errno
設(shè)置為<errno.h>
以字母E
開頭的后綴為大寫字母或數(shù)字的宏常量中列出的錯(cuò)誤代碼之一。
值errno
是0
在程序啟動(dòng)時(shí),盡管庫函數(shù)可以寫為正整數(shù)errno
是否發(fā)生了錯(cuò)誤,庫函數(shù)永遠(yuǎn)保存0
在errno
。
庫函數(shù)perror
,strerror
可用于獲取與當(dāng)前errno
值對(duì)應(yīng)的錯(cuò)誤條件的文本描述。
#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標(biāo)準(zhǔn)(ISO/IEC 9899:2011):
7.5錯(cuò)誤<errno.h>(p:205)
K.3.1.3使用errno(p:584)
K.3.2錯(cuò)誤<errno.h>(p:585)
C99標(biāo)準(zhǔn)(ISO/IEC 9899:1999):
7.5錯(cuò)誤<errno.h>(p:186)
C89 / C90標(biāo)準(zhǔn)(ISO/IEC 9899:1990):
4.1.3錯(cuò)誤<errno.h>
E2BIG,EACCES,...,EXDEV | 用于標(biāo)準(zhǔn)POSIX兼容錯(cuò)誤條件的宏(宏常量) |
---|---|
PERROR | 顯示當(dāng)前錯(cuò)誤對(duì)應(yīng)的字符串到stderr(函數(shù)) |
strerrorstrerror_sstrerrorlen_s(C11)(C11) | 返回給定錯(cuò)誤代碼(函數(shù))的文本版本 |
math_errhandlingMATH_ERRNOMATH_ERREXCEPT(C99)(C99)(C99) | 定義了常用數(shù)學(xué)函數(shù)(宏常量)使用的錯(cuò)誤處理機(jī)制, |