C There are three forms of error reporting usage in the Language standard library.
1 , errno
errno in the <errno.h> The header file is defined in the following
#ifndef Errnoextern int errno; #endif
external variable errno implementation-defined error codes in the Save Library program, Typically defined as errno.h e
all error codes are positive integers , the following example
# define EDOM / * Math argument out of domain of function. */
EDOM It means that the parameter is not in a field that the mathematical function can accept, and the macro is used later in the example.
errno A common use is to clear zeros before calling library functions and then check them.
2 , strerror
strerror in the <string.h> is defined in the following
__begin_namespace_std/* Return A string describing the meaning of the ' errno ' code in Errnum. */extern char *strerror (int __errnum) __THROW;__END_NAMESPACE_STD
function strerror Returns a pointer to an error message string, The content is defined by the implementation, and the string cannot be modified, but can be called in subsequent calls to strerror
3 , perror
perror in the <stdio.h> is defined in the following
__begin_namespace_std/* Print a message describing the meaning of the value of errno. This function was a possible cancellation point and therefore not marked with __throw. */extern void perror (const char *__s); __end_namespace_std
functionperrorprint the following sequence in the standard error output stream: parameter strings, colon, space, containingerrnothe error short message and line break in the current error code. In the standardClanguage, ifsis aNULLpointer orNULLa pointer to a character that prints only the wrong short message without printing the preceding argument strings, colons, and spaces.
Here are a few simple examples
#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 = perror ("sqrt failed");//sqrt failed:numerical argumen T out of the domain printf ("Error:%s\n", Strerror (errno));//error:numerical argument out of domain } return 0; }
errno (Error Reporting) usage in the C language