Home » C programs » stdio.h header file functions

perror() function of stdio.h in C



In this article, we are going to learn about the perror() function of stdio.h header file in C programming language, and use it prints a descriptive error message to stderr.
Submitted by Abhishek Sharma, on April 12, 2018

This function is very useful as it prints the error message on the screen. All we have to do is if you have a situation where we caught an error; call this function with a parameter of string.

The String which will be printed first, a colon, a space then the error message.

Example:

    perror("here is the error");
    will print →
    here is the error : ERROR MESSAGE.

stdio.h - perror() function Example in C

#include <stdio.h>

int main ()
 {

    // defining the file pointer
   FILE *f;

   // rename file name
   rename("abc.txt", "abcd.txt");

   // open file
   f = fopen("file.txt", "r");

   // condition if error then print error
   if( f == NULL ) {
      perror("Error in Code is: ");
      return(-1);
   }

   // close the file
   fclose(f);

   return(0);
}

Output

stdio.h - perror()  in c language






Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.