These functions help show error messages when something goes wrong in your program. They make it easier to understand what caused the problem.
perror and strerror functions
void perror(const char *s); char *strerror(int errnum);
perror prints your message s followed by a colon and the error description for the current errno.
strerror returns a string describing the error number you give it.
perror("File open error");errno value.printf("Error: %s\n", strerror(errno));ENOENT (usually "No such file or directory").int err = ENOENT; printf("Error message: %s\n", strerror(err));
This program tries to open a file that does not exist. It uses perror to print the error message, then prints the same message using strerror.
#include <stdio.h> #include <errno.h> #include <string.h> int main() { FILE *f = fopen("nonexistent.txt", "r"); if (!f) { perror("Failed to open file"); printf("Using strerror: %s\n", strerror(errno)); return 1; } fclose(f); return 0; }
errno is a global variable set by system calls and some library functions when an error happens.
Always include <errno.h> and <string.h> to use these functions.
perror prints directly to standard error, while strerror returns a string you can use anywhere.
perror prints an error message with your text and the system error.
strerror returns a string describing an error number.
Use these to make your program's errors clear and easy to understand.
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
