Error Conditions: <b>errno.h</b>




Document: The C Standard Library

next Utility Functions: stdlib.h
up The C Standard Library
gif Implementation-defined Limits: limits.h

Error Conditions: errno.h

Many functions in the standard library will detect "exception" or "error" conditions in certain circumstances - essentially if the function has been requested to do something which, for some reason, it can't. The exact action of the function in such situations depends on the details of the particular function and the particular exception condition. However, the most usual strategy is for the return value from the function to signal, with some special value, that something has gone wrong. It is then up to the calling site to react to this in some "appropriate" way. Minimally this will mean giving some kind of overt or "visible" external signal of the problem.

In any case, while the standard library functions typically provide an initial or gross indication that something has gone wrong via the return value, it is often also useful for the calling site to have access to more detailed information which clarifies exactly the nature of the problem. This is usually achieved by having the standard library function record a detailed "error number" in a global int variable called errno. This variable is defined within the standard library itself: but your programs can gain access to it by a so-called extern declaration. This declaration is already provided in the header file errno.h; so if you #include this, you will then be able to access errno just as any other variable is accessed. By examining its value immediately after a call to a standard function, your program can generally establish what (if anything) went wrong.

As well as the declaration of errno the file errno.h also provides a series of #define directives which define symbolic names for the standard error numbers or codes which may be recorded in errno. This would allow your program to test for specific error codes by comparing errno to these symbolic values. We shall also see later how errno can be automatically translated into a corresponding textual error "message", and, say, displayed, on the computer screen (see the discussion of the function perror(), prototyped in stdio.h).




Document: The C Standard Library

next Utility Functions: stdlib.h
up The C Standard Library
gif Implementation-defined Limits: limits.h



McMullin@eeng.dcu.ie
Fri Mar 29 14:35:38 GMT 1996