Easy Tutorial
❮ C Function Fmod C 100 Examples ❯

C Library Function - strerror()

C Standard Library - <string.h>

Description

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

Declaration

Here is the declaration for the strerror() function.

char *strerror(int errnum)

Parameters

Return Value

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

Example

The following example demonstrates the use 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'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 Function Fmod C 100 Examples ❯