A pointer to a string that serves as a custom prefix for the error message. If `str` is `NULL` or an empty string, no prefix is printed.
Read more about parameters of perror in parameters section
The perrorfunction in C language Prints a descriptive error message to stderr based on the current value of the C global variable errno.
The perror function produces a message on the standard error output, describing the last error encountered during a call to a system or library function. It first outputs the string pointed to by s, followed by a colon and a space. Then, it outputs the appropriate error message string corresponding to the current value of errno, followed by a newline character.
The perrorfunction takes 1
parameter:
•
const char * `str`: A pointer to a string that serves as a custom prefix for the error message. If `str` is `NULL` or an empty string, no prefix is printed.
Writes a custom error message to the standard error stream, combining the string provided in `str` (if not `NULL`) with a description of the last error encountered (stored in the global variable `errno`). This function is commonly used for debugging and reporting runtime errors.
The perror function return value :
This function does not return a value
Output
This example demonstrates how perror is used to print an error message when a file cannot be opened. The custom prefix 'Error opening file' is provided.