The errno is a macro defined in the errno.h header file. When anything went wrong the errno macro is set by the system calls and some library functions.
extern int errno;
The errno macro does not take any parameters. The errno is an integer variable that holds the error code for reporting the cause of error.
The macro errno does not return any value. At the starting of the program, the errno value is set to zero. By some library functions, the value of errno is set to some positive value to report a specific error condition.
#include <stdio.h>
#include <errno.h>
#include <string.h>
extern int errno ;
int main () {
FILE *pnt;
pnt = fopen("myfile.txt", "r");
if( pnt == NULL ) {
fprintf(stderr, "Value of errno is: %d\n", errno);
fprintf(stderr, "Error opening file is: %s\n", strerror(errno));
} else {
fclose(pnt);
}
return(0);
}
Output:
Value of errno is: 2 Error opening file is: No such file or directory