Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      errno

      From cppreference.com
      <c‎ |error
       
       
      Error handling
      Error codes
      Error codes
      errno
      Assertions
      (C11)(removed in C23)
      Bounds checking
       
      Defined in header<errno.h>
      #define errno /* implementation-defined */

      errno is a preprocessor macro (but see note below) that expands to athread-local(since C11) modifiable lvalue of typeint. Several standard library functions indicate errors by writing positive integers toerrno. Typically, the value oferrno is set to one of the error codes listed in<errno.h> as macro constants beginning with the letterE followed by uppercase letters or digits.

      The value oferrno is0 at program startup, and although library functions are allowed to write positive integers toerrno whether or not an error occurred, library functions never store0 inerrno.

      Library functionsperror andstrerror can be used to obtain textual descriptions of the error conditions that correspond to the currenterrno value.

      Note: Until C11, the C standards had contradictory requirements, in that they said thaterrno is a macro butalso that "it is unspecified whethererrno is a macro or an identifier declared with external linkage". C11 fixes this, requiring that it be defined as a macro (see also WG14N1338).

      [edit]Example

      Run this code
      #include <errno.h>#include <math.h>#include <stdio.h> void show_errno(void){constchar*err_info="unknown error";switch(errno){caseEDOM:            err_info="domain error";break;caseEILSEQ:            err_info="illegal sequence";break;caseERANGE:            err_info="pole or range error";break;case0:            err_info="no error";}fputs(err_info,stdout);puts(" occurred");} int main(void){fputs("MATH_ERRNO is ",stdout);puts(math_errhandling& MATH_ERRNO?"set":"not set"); errno=0;(void)(1.0/0.0);    show_errno(); errno=0;(void)acos(+1.1);    show_errno(); errno=0;(void)log(0.0);    show_errno(); errno=0;(void)sin(0.0);    show_errno();}

      Possible output:

      MATH_ERRNO is setno error occurreddomain error occurredpole or range error occurredno error occurred

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.5 Errors <errno.h> (p: TBD)
      • K.3.1.3 Use of errno (p: TBD)
      • K.3.2 Errors <errno.h> (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.5 Errors <errno.h> (p: TBD)
      • K.3.1.3 Use of errno (p: TBD)
      • K.3.2 Errors <errno.h> (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.5 Errors <errno.h> (p: 205)
      • K.3.1.3 Use of errno (p: 584)
      • K.3.2 Errors <errno.h> (p: 585)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.5 Errors <errno.h> (p: 186)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.1.3 Errors <errno.h>

      [edit]See also

      macros for standard POSIX-compatible error conditions
      (macro constant)[edit]
      displays a character string corresponding of the current error tostderr
      (function)[edit]
      returns a text version of a given error code
      (function)[edit]
      defines the error handling mechanism used by the common mathematical functions
      (macro constant)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/error/errno&oldid=180045"

      [8]ページ先頭

      ©2009-2025 Movatter.jp