Errno global variable
Many system functions record the error cause in the global variable errno defined by libc when returning an error. Each error cause corresponds to an error code. For more information, see the Man Page of errno (3, errno is in the header file errno. h declares that it is an integer variable and all error codes are positive integers.
For example, if fopen fails to open a file, the function returns NULL and records the cause of the error in the global variable errno.
If the errno variable is printed directly when an error message is printed in the program, only an integer is printed, and no error is displayed. The better way is to use the perror or strerror function to interpret errno as a string and print it again.
Perror Function
# Include <stdio. h>
Void perror (const char * s );
The perror function prints the error information to the standard error output. First, print the string indicated by parameter s, then print the ":" sign, and then print the cause of the Error Based on the current errno value. For example:
Example: perror
# Include <stdio. h>
# Include <stdlib. h>
Int main (void)
{
FILE * fp = fopen ("abcde", "r ");
If (fp = NULL ){
Perror ("Open file abcde ");
Exit (1 );
}
Return 0;
}
If the file abcde does not exist, fopen returns-1 and sets errno to ENOENT. Then the perror function reads the value of errno, interprets ENOENT as a string of No such file or directory, and prints it, the final output is Open file abcde: No such file or directory. Although perror can print out the cause of the error, the string parameter passed to perror should still provide some additional information so that when you see the error message, you can quickly locate the error in the program, if there are many fopen calls in the program, and each fopen opens different files, it is very helpful to print the file name in each fopen error handling process.
If you change the above program to the following:
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
Int main (void)
{
FILE * fp = fopen ("abcde", "r ");
If (fp = NULL ){
Perror ("Open file abcde ");
Printf ("errno: % d \ n", errno );
Exit (1 );
}
Return 0;
}
The error code printed by printf is not the error code generated by fopen, but the error code generated by perror. Errno is a global variable. Many system functions change it. The ERRORS section in the fopen function Man Page describes the error codes it may produce. The Man Page of The perror function does not contain ERRORS, it does not produce error codes, but other functions it calls may also change the errno variable. Most system functions have a Side Effect, that is, they may change the errno variable (of course there are a few exceptions, such as strcpy). Therefore, if a system function returns an error, check errno immediately, you cannot call other system functions before checking errno.
Strerror Function
The strerror function returns a string of error causes based on the error number.
# Include <string. h>
Char * strerror (int errnum );
Returned value: the string corresponding to the error code errnum.
This function returns a pointer to the static memory. When learning the thread library, we will see that some function error codes are not stored in errno, but returned through the return value, so we cannot call perror to print the cause of the error, in this case, strerror comes in handy:
Fputs (strerror (n), stderr );
Author: kevinzhangyang