Friday, October 30, 2020

,

Program for perror function in c - library function

 perror is a c library function in c this function is used to print a descriptive error message in c.

Program for perror library function in c
Program for perror function

 

Syntax:-

void perror(const char *str)

  • str in c contain a custom message that is printed before the error message itself.
  • perror function in c doesn't return any value

Example Program for perror function in c

#include <stdio.h>

int main () {
   FILE *f;

 
   /* first rename the file */
   rename("file.txt", "updatefile.txt");

   /* now let's open same file */
   f = fopen("file.txt", "r");
   if( fp == NULL ) {
      perror("Error: ");
      return(-1);
   }
   fclose(fp);
      
   return(0);
}

Output

Error: : No such file or directory

0 comments:

Post a Comment