Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function – strerror()

C Library Function - strerror()

C Standard Library - <string.h> C Standard Library -

Description

C Library Function **char *strerror(int errnum) ** searches the internal array for the error number errnum, and returns a pointer to the error message string.strerror The generated error string depends on the development platform and compiler.

Declaration

Below is the declaration of the strerror() function.



char *strerror(int errnum)

Parameters

Return Value

This function returns a pointer to an error string that describes the error errnum.

Example

The following example demonstrates the use of the strerror() function.



#include 

#include 

#include 



int main ()

{

   FILE *fp;



   fp = fopen("file.txt","r");

   if( fp == NULL ) 

   {

      printf("Error: %s\n", strerror(errno));

   }

   

  return(0);

}

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



Error: No such file or directory

C Standard Library - <string.h> C Standard Library -