Errno can help us find the error message of the system function. For example, the open function, if returned normally, its return value is a non-negative integer. The exception will return-1, and the system function will set the value of errno, so that we can understand the cause of the error.
The correct use of errno is to first clarify the system function call exception (such as return 1 or null), and then to determine the value of the errno. We cannot directly judge the value of errno to determine whether a system function call has occurred. Because the errno value is not zeroed, it only assigns the corresponding value when an exception occurs. And of course we don't have to clear it ourselves.
In addition, errno is thread-safe. It can be understood that each thread has a errno value that does not interfere with each other.
There are two system functions that can make the error message more readable, as follows:
#include <string.h>
char *strerror (int errnum);
Returns:pointer to message string
#include <stdio.h>
void perror (const char *msg);
by judging the value of errno, we can make our system more robust. One example is that some operating systems will not continue to block in the previous system function when the signal is processed at slow systems call, while setting the errno value to eintr. Another example is when the network connection is abnormal, we can slow down for a period of time and then connect without having to exit directly.
My opinion of errno