参考:
NAME
errno - number of last error
SYNOPSIS
#include <errno.h>
DESCRIPTION
The <errno.h> header file defines the integer variable errno, which is set by system calls and some library functions in the event of an
error to indicate what went wrong. Its value is significant only when the return value of the call indicated an error (i.e., -1 from most
system calls; -1 or NULL from most library functions); a function that succeeds is allowed to change errno(没太看懂).
Valid error numbers are all nonzero; errno is never set to zero by any system call or library function(只有出错后,才会设置errno,后续检查先判断api返回值,如果出错误,再检查errno,或者使用strerror读出具体错误).
For some system calls and library functions (e.g., getpriority(2)), -1 is a valid return on success. In such cases, a successful return
can be distinguished from an error return by setting errno to zero before the call, and then, if the call returns a status that indicates
that an error may have occurred, checking to see if errno has a nonzero value.
errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared; errno may be a macro.
errno is thread-local; setting it in one thread does not affect its value in any other thread.(线程安全,不用担心被打断后,errno被其他线程修改)
bad show:
实测可知:如果出错后,不手动重置错误码,后续会一直保留此错误
good show:
建议读出错误码后,将此错误码赋值到临时变量,然后将errno设置为0,最后再打印出临时变量的错误信息