C library functions -strerror()

C Standard Library - <string.h>

Description

C Library Functionschar *strerror(int errnum) Search the error number from the internal arrayerrnum, and returns a pointer to the error message string.strerrorThe generated error string depends on the development platform and compiler.

Declaration

The following is the declaration of the strerror() function.

char *strerror(int errnum)

Parameter

  • errnum-- Error number, usuallyerrno。

Return Value

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

Example

The following example demonstrates the usage of the 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, which will produce the following result because we attempt to open a file that does not exist:

Error: No such file or directory

C Standard Library - <string.h>

other extensions