perror and strerror functions - Time & Space Complexity
We want to understand how the time cost changes when using perror and strerror functions in C.
How does the work these functions do grow as error messages or codes change?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {
FILE *file = fopen("nonexistent.txt", "r");
if (!file) {
perror("File open error");
const char *msg = strerror(errno);
printf("Error message: %s\n", msg);
}
return 0;
}
This code tries to open a file that does not exist, then prints the error using perror and strerror.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The functions perror and strerror internally look up error messages.
- How many times: Each function is called once here, so the lookup happens once per call.
These functions do a simple lookup of an error message based on an error code.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to find message |
| 100 | About 100 steps to find message |
| 1000 | About 1000 steps to find message |
Pattern observation: The time to find the error message grows roughly in direct proportion to the number of possible error codes.
Time Complexity: O(n)
This means the time to get the error message grows linearly with the number of error codes the system knows about.
[X] Wrong: "perror and strerror run instantly no matter how many errors exist."
[OK] Correct: These functions search through error messages, so if there are many, it takes more time.
Knowing how standard library functions like perror and strerror work helps you understand performance in real programs.
"What if strerror used a hash table for error messages? How would the time complexity change?"
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
