What if your program could explain its errors in plain words, saving you hours of confusion?
Why perror and strerror functions? - Purpose & Use Cases
Imagine you write a program that opens a file, but it fails. You want to tell the user what went wrong. Without special help, you might just say "Error happened" without details.
Manually guessing or printing error codes is confusing and slow. You have to look up what each error number means, which wastes time and can cause mistakes. Users get unclear messages, making problems hard to fix.
The perror and strerror functions automatically translate error codes into clear, readable messages. They save you from guessing and give users helpful info instantly.
printf("Error code: %d\n", errno);perror("File open failed"); // or printf("Error: %s\n", strerror(errno));
They let your program explain problems clearly and quickly, making debugging and user support much easier.
When your program tries to open a missing file, perror prints "File open failed: No such file or directory" instead of just "Error happened".
perror prints a message with the error reason automatically.
strerror returns the error message as a string you can use anywhere.
Both help make error messages clear and user-friendly.
Practice
perror function do in C?Solution
Step 1: Understand
perrorpurposeperrorprints a message you provide, then adds the system error message related to the last error.Step 2: Compare with other options
strerrorreturns a string but does not print; program termination or clearing errors are unrelated.Final Answer:
Prints a custom message followed by the system error message -> Option AQuick Check:
perror prints message + system error [OK]
- Confusing perror with strerror
- Thinking perror returns a string
- Assuming perror stops the program
perror with the text "File error"?Solution
Step 1: Check correct string syntax in C
Strings must be in double quotes, so "File error" is correct.Step 2: Validate
perrorusageperrortakes a const char* string, soperror("File error")is correct syntax.Final Answer:
perror("File error"); -> Option CQuick Check:
Use double quotes for strings in perror [OK]
- Using single quotes instead of double quotes
- Passing variable names without quotes
- Missing parentheses in function call
errno is set to ENOENT (No such file or directory)?
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {
printf("Error: %s\n", strerror(errno));
return 0;
}Solution
Step 1: Understand
strerror(errno)strerrorreturns a string describing the error number stored inerrno.Step 2: Match
errnovalue to messageENOENTmeans "No such file or directory", sostrerror(errno)returns that message.Final Answer:
Error: No such file or directory -> Option DQuick Check:
strerror(ENOENT) = "No such file or directory" [OK]
- Confusing error messages for different errno values
- Assuming strerror prints instead of returns string
- Not including header
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {
int errnum = ENOENT;
printf("Error: %s\n", perror(errnum));
return 0;
}Solution
Step 1: Check
perrorreturn typeperrorprints directly to stderr and returns void; it does not return a string.Step 2: Analyze usage inside printf
Passingperror(errnum)insideprintfis invalid becauseperrorreturns void, not a string.Final Answer:
Using perror inside printf is incorrect; perror prints directly and returns void -> Option AQuick Check:
perror prints itself, don't use inside printf [OK]
- Using perror inside printf expecting a string
- Confusing perror and strerror return types
- Ignoring compiler warnings about void return
perror and strerror together to achieve this?
FILE *open_file(const char *filename) {
FILE *f = fopen(filename, "r");
if (!f) {
// What to do here?
}
return f;
}Solution
Step 1: Understand error reporting with perror
perrorprints your message plus the system error, making it clear why fopen failed.Step 2: Compare options for clarity
Callperror("Failed to open file")to print error, then return NULL usesperror("Failed to open file"), which is clear and standard. Printprintf("Error: %s\n", strerror(errno))only, then return NULL prints only strerror message without context. Callperror(filename)without message, then return NULL prints filename only, which is less clear. Return NULL without printing any error message prints nothing.Final Answer:
Call perror("Failed to open file") to print error, then return NULL -> Option BQuick Check:
Use perror with custom message for clear errors [OK]
- Printing strerror without context message
- Using perror with filename only, confusing output
- Not printing any error message on failure
Ready to prove it?
Pass the test and this pattern moves unknown
