errno (Error Reporting) usage in the C language

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.