Easy Tutorial
❮ C Examples Add Numbers C Function Sscanf ❯

C Library Macro - errno

C Standard Library - <errno.h>

Description

The C library macro extern int errno is set by system calls and some library functions in the event of an error to indicate what went wrong.

Declaration

Here is the declaration for the errno macro.

extern int errno

Parameters

Return Value

Example

The following example demonstrates the usage of the errno macro.

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

extern int errno ;

int main ()
{
   FILE *fp;

   fp = fopen("file.txt", "r");
   if( fp == NULL ) 
   {
      fprintf(stderr, "Value of errno: %d\n", errno);
      fprintf(stderr, "Error opening file: %s\n", strerror(errno));
   }
   else 
   {
      fclose(fp);
   }

   return(0);
}

Let's compile and run the above program, which will produce the following result when the file file.txt does not exist:

Value of errno: 2
Error opening file: No such file or directory

C Standard Library - <errno.h>

❮ C Examples Add Numbers C Function Sscanf ❯