The function perror () in C

The function void perror (const char * str) in the C Library standard prints a message describing the error to stderr. First, the printed string str is followed by a colon and then a space.

The function void perror (const char * str) in the C Library standard prints a message describing the error to stderr. First, the printed string str is followed by a colon and then a space.

Declare the perror () function in C

Here is the declaration for the perror () function in C:

 void perror ( const char * str ) 

Parameters

str - This is the string containing a Custom message to be printed before the error message.

Returns the value

This function does not return any values.

For example

The following C program illustrates the usage of the perror () function in C:

 #include int main () { FILE * fp ; /* Doi ten file */ rename ( "baitapc.txt" , "newbaitapc.txt" ); /* bay gio thu co gang mo baitapc.txt */ fp = fopen ( "baitapc.txt" , "r" ); if ( fp == NULL ) { perror ( "Error: " ); return (- 1 ); } fclose ( fp ); return ( 0 ); } 

Compiling and running the above C program will result: (because we're trying to open a file that doesn't exist)

The function perror () in C Picture 1The function perror () in C Picture 1

According to Tutorialspoint

Previous lesson: Function ungetc () in C

Next lesson: stdlib.h in C

3.6 ★ | 7 Vote