Detailed analysis of error reporting errno and its related application in C language _c language

Source: Internet
Author: User
Tags error code

There are three types of error reporting usages in the C language standard library.
1, errno
errno is defined in the <errno.h> header file as follows

#ifndef errno 
extern int errno; 
#endif 

An external variable errno an implementation-defined error code in a save library program, usually defined as a macro that starts with E in errno.h.
All error codes are positive integers, as shown in the following example

# define EDOM/  * Math argument out of domain of function. 

Edom means that the parameter is not in a field that the math function can accept, and a later example uses the macro.
A common use for errno is to clear zero before calling the library function, and then check again.

Errno is a very useful dynamic when programming with C in Linux. He can keep the error code of the method of the last call to C. But if the last successful call to C's method, errno will not change. Therefore, the errno is detected only when the C language function returns a value exception.
errno returns a number with each number representing an error type. A detailed view of the header file is available. /usr/include/asm/errno.h
How do I convert a errno number into a corresponding text description?

A simple example

#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 Argume NT out of domain 
  printf ("Error:%s\n", Strerror (errno));//error:numerical argument out of domain 
 } 
 
 retur n 0; 

 

2, Strerror
the strerror is defined in <string.h> as follows
__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 whose contents are defined by the implementation and the string cannot be modified, but can be overwritten after subsequent calls to the Strerror function.

char *strerror (int errno)

The use of the following methods:

fprintf (stderr, "Error in CreateProcess%s, Process ID%d", strerror (errno), ProcessID)

Converts the error code to a string error message, which you can combine to output the string and other information to the user interface.
Note: Suppose ProcessID is an already-acquired cosmetic ID

3, Perror
the perror is defined in <stdio.h> as follows
__begin_namespace_std
/* Print a message describing the meaning of the value of errno.
This function is a possible cancellation point and therefore not
Marked with __throw. */
extern void perror (const char *__s);
__end_namespace_std
Function Perror Prints the following sequence in the standard error output stream: parameter string s, colon, space, error short message containing the current error code in the errno, and line breaks. In standard C, if S is a pointer to a null pointer or null character, only the error message is printed, not the preceding argument string s, colon, and space.

void perror (const char *s)

Function description
Perror () is used to output the error of the previous function to the standard error (STDERR), the string that the parameter S refers to is printed first, followed by the error reason string. This error reason determines the string to output according to the value of the global variable errno.
In addition, not all error messages that occur with C function calls modify errno. such as the gethostbyname function.
is the errno thread safe?
errno is thread-safe and, generally speaking, the compiler automatically guarantees errno security.
Let's look at the related header file/usr/include/bits/errno.h
You will see the following:

# if!defined _libc | | Defined _libc_reentrant
/* When using threads, errno is a per-thread value. * * Define
errno (*__errno_location ( )
# endif # endif/*!__assembler__ */
#endif/
* _errno_h * *

That is, errno is multithreaded/process-safe when there is no definition of __libc or definition of _libc_reentrant.
To check whether your compiler defines the above variables, you might want to use one of the following simple programs.

#include <stdio.h>
#include <errno.h>
 
int main (void)
{
#ifndef __assembler__
  printf ("Undefine __assembler__/n");
#else
  printf ("define __assembler__/n");
#endif
 
#ifndef __libc
  printf ("Undefine __libc/n");
#else
  printf ("define __libc/n");
#endif
 
#ifndef _libc_reentrant
  printf ("Undefine _libc_reentrant/n");
#else
  printf ("define _libc_reentrant/n");
#endif return
 
  0;
}

Related Article

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.