The standard says this about errno
:
The value of errno is zero at program startup, but is never set to zero by any library
function. The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard.
Which says to me that any library function can screw around with errno
in any way it likes except:
- it can't set
errno
to 0
- it can't do what it likes if the standard explicitly says otherwise
Note that the standard suggests the following in a footnote:
Thus, a program that uses errno
for error checking should set it to zero before a library function call, then inspect it before a subsequent library function call. Of course, a library function can save the value of errno
on entry and then set it to zero, as long as the original value is restored if errno
's value is still zero just before the return.
As noted in other answers, it's common for functions that are not in the standard to set errno
as well.