Sunday, October 5, 2014

errno

The return code (commonly -1) in C informed the operation has failed.  Specific failure condition is notified via the variable errno

extern int errno

perror prints the textual description indicated by errno.

void perror (const char *str);


The string will be printed with following colon preceding the error description message.

Another function provided by C is strerror.  This function return a pointer to the description message.  The function is not thread safe as the message buffer returned could be modified by subsequent strerror or perror call.  strerror_r is a thread safe version which accept an externally allocated buffer as argument in which to place the error description string.

errno must be set to 0 before it is used (i.e. before making call).

No comments: