Understanding Strerror() Function in C

Understanding Strerror() Function in C

3 mins read272 Views Comment
Updated on Oct 3, 2023 11:55 IST

This blog covers strerror() Function in C programming. We have explained this concept with the help of examples.

2023_03_Copy-of-Feature-Image-Templates-7-1.jpg

In this tutorial, we will discuss the strerror() function in C programming language that returns a string describing an error code. This can be especially helpful when dealing with file I/O operations or other functions that return error codes. We will be covering the following sections: 

So, without further ado, let’s get started! 

Explore: C Programming Courses

Introduction to Strerror() Function in C 

The strerror() function is a standard library function in C that is defined in the string.h header file. The function returns a string describing an error code that has occurred in a program.  

Here is the syntax for the strerror() function: 

 
char *strerror(int errnum);
Copy code
Recommended online courses

Best-suited Programming courses for you

Learn Programming with these high-rated online courses

β‚Ή10 K
3 months
β‚Ή19.99 K
97 days
– / –
6 weeks
β‚Ή4 K
2 months
β‚Ή15 K
3 months
β‚Ή2.05 K
3 months
β‚Ή31.55 K
7 months
β‚Ή26 K
3 months

Parameter of Strerror() 

The strerror() function in C takes a single parameter errnum, an integer value representing the error code that occurred. 

Return Value of strerror() 

Using strerror(), a string describing the error will be returned.  

The strerror() function is often used in conjunction with other functions that return error codes. Such as open() or read(), to provide more information about the cause of the error. For example, if a call to open() fails, the error code can be passed to strerror() to get a string that describes the reason for the failure. 

Examples of strerror() Function in C

Example 1: Using strerror() with global variable errno  

 
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {
FILE *fp;
fp = fopen("nonexistent_file.txt", "r");
if (fp == NULL) {
printf("Error opening file: %s\n", strerror(errno));
} else {
printf("File opened successfully.\n");
fclose(fp);
}
return 0;
}
Copy code

Output: 

Error opening file: No such file or directory  

This example shows how to use the fopen() function to open an existing file. If fopen() fails, it sets the global variable errno to an error code that describes the reason for the failure. We pass this error code to the strerror() function to get a string that describes the error. If fopen() succeeds, we print a message saying that the file was opened successfully and then close the file.  

Example 2: Using strerror() with a custom error code  

 
#include <stdio.h>
#include <string.h>
int main() {
int errnum = 42;
char *errstr = strerror(errnum);
printf("Error message: %s\n", errstr);
return 0;
}
Copy code

Output: 

Error message: No message of desired type  

In this example, we are using the strerror() function to get a string that describes a custom error code (in this case, 42). We pass the error code to strerror() and then print the resulting string to the console. 

Note that in both examples, the strerror() function returns a pointer to a string that describes the error. This string should not be modified or freed by the program. Because it is stored in a static buffer that is managed by the strerror() function.  

Example 3: Using strerror() with a custom error code and a switch statement 

 
#include <stdio.h>
#include <string.h>
int main() {
int errnum = 5;
char *errstr;
switch(errnum) {
case 1:
errstr = "Error 1: File not found";
break;
case 2:
errstr = "Error 2: Invalid argument";
break;
case 3:
errstr = "Error 3: Permission denied";
break;
default:
errstr = strerror(errnum);
break;
}
printf("Error message: %s\n", errstr);
return 0;
}
Copy code

Output: 

Error message: Input/output error  

In this example, we are using a switch statement to determine which error message to display based on a custom error code. If the error code matches one of the cases in the switch statement. We use a string literal to set the errstr variable to a custom error message. Otherwise, we pass the error code to strerror() to get the error message as a string. 

Note that the strerror() function is particularly useful when you need to provide more information about an error code than the error code provides. Using strerror(), you can get a human-readable string that describes the error. It can be helpful when debugging your code or providing user feedback.  

Understanding the Difference Between Structure and Union in C
Switch Case in C Programming: A Comprehensive Guide
Variables in C Programming: Types, Scope, and Lifetime

Endnotes 

In conclusion, by using strerror(), you can provide more detailed information about the error to the user or to the programmer debugging the code. 

Hope this article was helpful for you. If you want to learn more about C programming and solidify your basics, you can explore our articles on C. 

Contributed by: Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio