C library function - strerror()

Advertisements


Description

The C library function char *strerror(int errnum) searches an internal array for the error number errnum and returns a pointer to an error message string. The error strings produced by strerror depend on the developing platform and compiler.

Declaration

Following is the declaration for strerror() function.

char *strerror(int errnum)

Parameters

  • errnum -- This is the error number, usually errno

Return Value

This function returns a pointer to the error string describing error errnum.

Example

The following example shows the usage of strerror() function.

#include <stdio.h>
#include <string.h>
#include <errno.h>

int main ()
{
   FILE *fp;

   fp = fopen("file.txt","r");
   if( fp == NULL ) 
   {
      printf("Error: %s\n", strerror(errno));
   }
   
  return(0);
}

Let us compile and run the above program, this will produce the following result because we are trying to open a file which does not exist:

Error: No such file or directory


Advertisements
Advertisements