Understanding Strerror() Function in C
This blog covers strerror() Function in C programming. We have explained this concept with the help of examples.
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);
Best-suited Programming courses for you
Learn Programming with these high-rated online courses
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; }
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; }
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; }
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.
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
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