Many library routines return a value that indicates an error condition. To avoid unexpected results, your code should always check such error values and handle all of the possible error conditions. The description of each library routine in the reference section lists the routine's return value(s).
Some library functions do not have a set error return. These include functions that return nothing and functions whose range of return values makes it impossible to return a unique error value.
To aid in error handling, some functions set the value of a global variable named errno. If the reference description of a routine states that it sets the errno variable, you can use errno in two ways:
Compare errno to the values defined in the header file ERRNO.H.
Handle errno with the perror or strerror library routine. The perror routine prints a system error message to the standard error (stderr). The strerror routine stores the same information in a string for later use.
When you use errno, perror, and strerror, remember that the value of errno reflects the error value for the last call that set errno. To avoid confusion, you should always test the return value to verify that an error actually occurred. Once you determine that an error has occurred, use strerror or perror immediately. Otherwise, the value of errno may be changed by intervening calls.
Library math routines set errno by calling the _matherr or _matherrl library routine; both are described in the reference section. If you wish to handle math errors differently from these routines, you can write your own routine and name it _matherr or _matherrl. Your routine must follow the rules listed in the _matherr reference description.
The ferror library routine allows you to check for errors in stream input/output operations. This routine checks if an error indicator has been set for a given stream. Closing or rewinding the stream automatically clears the error indicator. You can also reset the error indicator by calling the clearerr library routine.
The feof library routine tests for end-of-file on a given stream. An end-of-file condition in low-level input and output can be detected with the _eof routine or when a _read operation returns 0 as the number of bytes read.
The _grstatus library routine allows you to check for errors after calling certain graphics library operations. See the reference page on the _grstatus function for details.