C perror() function
C library function - perror()
The perror() function is used to print an error message to stderr based on the error state stored in errno. The string pointed to by string, followed by a colon and a space, is printed to the standard error stream if string is not NULL.
Syntax:
void perror(const char *str)
Parameters:
Name | Description | Required /Optional |
---|---|---|
str | Contains a custom message. | Required |
Return value
- This function does not return any value.
Example: The following example shows the usage of perror() function.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fh;
if ((fh = fopen("test1.txt","r")) == NULL)
{
perror("Can not open the said file.");
}
return 0;
}
Output:
Can not open the said file.: No such file or directory
C Programming Code Editor:
Previous C Programming: C puts()
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join