The perror()
function displays the description of the error that corresponds to an error code stored in the system variable errno
.
errno
is a system variable which holds the error code that describes the error condition. This error condition is produced by a call to a library function.
str
: The pointer to a null-terminated string. This string should contain an explanatory message about the error.The perror()
function outputs the error description by combining the following in the given order:
str
.errno
.#include <stdio.h>int main () {FILE *fp;/* If we try to open a file that does not existthe variable errno will be set accordingly. */fp = fopen("file.txt", "r");if( fp == NULL ) {// The following string is prepended to the error description.perror("The error is ");return(-1);}fclose(fp);return(0);}
Free Resources