0
0
Cprogramming~20 mins

perror and strerror functions - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of perror after a failed fopen?
Consider the following C code snippet. What will be printed to the standard error when fopen fails to open a non-existent file?
C
#include <stdio.h>
#include <errno.h>

int main() {
    FILE *fp = fopen("nonexistent.txt", "r");
    if (!fp) {
        perror("Error opening file");
    }
    return 0;
}
AError opening file: File exists
BError opening file: Permission denied
CError opening file: Unknown error
DError opening file: No such file or directory
Attempts:
2 left
💡 Hint
The file does not exist, so fopen sets errno to ENOENT.
Predict Output
intermediate
2:00remaining
What does strerror return for errno 13?
What string does strerror return when errno is set to 13?
C
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main() {
    errno = 13;
    printf("%s\n", strerror(errno));
    return 0;
}
ABad file descriptor
BNo such file or directory
CPermission denied
DInput/output error
Attempts:
2 left
💡 Hint
Errno 13 corresponds to a permission problem.
🔧 Debug
advanced
2:00remaining
Why does this perror call print an empty message?
Examine the code below. Why does perror print only a newline without any message?
C
#include <stdio.h>
#include <errno.h>

int main() {
    errno = 0;
    perror("");
    return 0;
}
ABecause the string passed to perror is empty, it causes a runtime error.
BBecause errno is zero, perror prints only a newline with no message.
CBecause perror requires a non-empty string, this code causes a segmentation fault.
DBecause errno is zero, perror prints 'Success' message.
Attempts:
2 left
💡 Hint
Check what errno value means zero and how perror behaves with empty string.
📝 Syntax
advanced
2:00remaining
Which option correctly uses strerror to print an error message?
Select the code snippet that correctly prints the error message for the current errno using strerror.
Aprintf("Error: %s\n", strerror(errno));
Bprintf("Error: %s\n", strerror);
Cprintf("Error: %s\n", strerror());
Dprintf("Error: %s\n", strerror(errno()));
Attempts:
2 left
💡 Hint
strerror requires an integer error code as argument.
🚀 Application
expert
3:00remaining
How to safely print a custom error message with strerror in a multithreaded program?
In a multithreaded C program, which approach is safest to print a custom error message with the error string corresponding to errno?
AUse strerror_r(errno, buffer, size) to get the error string, then print it with the custom message.
BManually map errno values to strings in a global array and print from there.
CUse strerror(errno) directly and print it with the custom message, ignoring thread safety.
DUse perror with a custom message directly, since it is thread-safe.
Attempts:
2 left
💡 Hint
strerror is not thread-safe, but strerror_r is designed for thread safety.