Latest web development tutorials

C library macro - errno

C standard library - <errno.h> C standard library - <errno.h>

description

C library macroextern int errno is set by system calls, library functions in some error event indicates what error has occurred.

statement

Here is the errno macro statement.

extern int errno

parameter

  • NA

return value

  • NA

Examples

The following example demonstrates errno macro usage.

#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, when the filefile.txt does not exist, the following results:

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

C standard library - <errno.h> C standard library - <errno.h>