Node:Checking for Errors, Next:Error Codes, Up:Error Reporting
Most library functions return a special value to indicate that they have
failed. The special value is typically -1
, a null pointer, or a
constant such as EOF
that is defined for that purpose. But this
return value tells you only that an error has occurred. To find out
what kind of error it was, you need to look at the error code stored in the
variable errno
. This variable is declared in the header file
errno.h
.
volatile int errno | Variable |
The variable errno contains the system error number. You can
change the value of errno .
Since The initial value of Many library functions can set Portability Note: ISO C specifies There are a few library functions, like |
All the error codes have symbolic names; they are macros defined in
errno.h
. The names start with E
and an upper-case
letter or digit; you should consider names of this form to be
reserved names. See Reserved Names.
The error code values are all positive integers and are all distinct,
with one exception: EWOULDBLOCK
and EAGAIN
are the same.
Since the values are distinct, you can use them as labels in a
switch
statement; just don't use both EWOULDBLOCK
and
EAGAIN
. Your program should not make any other assumptions about
the specific values of these symbolic constants.
The value of errno
doesn't necessarily have to correspond to any
of these macros, since some library functions might return other error
codes of their own for other situations. The only values that are
guaranteed to be meaningful for a particular library function are the
ones that this manual lists for that function.
On non-GNU systems, almost any system call can return EFAULT
if
it is given an invalid pointer as an argument. Since this could only
happen as a result of a bug in your program, and since it will not
happen on the GNU system, we have saved space by not mentioning
EFAULT
in the descriptions of individual functions.
In some Unix systems, many system calls can also return EFAULT
if
given as an argument a pointer into the stack, and the kernel for some
obscure reason fails in its attempt to extend the stack. If this ever
happens, you should probably try using statically or dynamically
allocated memory instead of stack memory on that system.