Error Handling for System Functions
If an error occurs during a call to a
system function, the global error variable errno
is assigned an appropriate error code. The following three functions
are used to provide the corresponding system error messages:
-
voidDeclared in stdio.hperror( const char *string); Writes the text pointed to by
string, followed by the system error message corresponding to the current value oferrno, to the standard error stream.-
char *Declared in string.hstrerror( interrnum); Returns a pointer to the system error message corresponding to
errnum. The value oferrnumis usually obtained from the error variableerrno.The following two statements result in the same output:
perror( "OPEN" ); fprintf( stderr, "OPEN: %s\n", strerror( errno ) );
-
voidassert( intexpression); Declared in assert.h This macro tests the scalar expression
expression. If the result is 0, or “false”, thenassert()writes the expression, function name, filename, and line number to the standard error stream, and then aborts program. If the expression is “true” (i.e., not equal to 0), no action is taken and the program continues.If the macro
NDEBUGis defined, calls toassert()have no effect.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access