Bird
Raised Fist0
Cprogramming~20 mins

perror and strerror functions - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the perror function do in C?
easy
A. Prints a custom message followed by the system error message
B. Returns a string describing an error number
C. Terminates the program immediately
D. Clears the error state of the program

Solution

  1. Step 1: Understand perror purpose

    perror prints a message you provide, then adds the system error message related to the last error.
  2. Step 2: Compare with other options

    strerror returns a string but does not print; program termination or clearing errors are unrelated.
  3. Final Answer:

    Prints a custom message followed by the system error message -> Option A
  4. Quick Check:

    perror prints message + system error [OK]
Hint: perror prints errors; strerror returns error strings [OK]
Common Mistakes:
  • Confusing perror with strerror
  • Thinking perror returns a string
  • Assuming perror stops the program
2. Which of the following is the correct syntax to print an error message using perror with the text "File error"?
easy
A. perror(File_error);
B. perror(File error);
C. perror("File error");
D. perror('File error');

Solution

  1. Step 1: Check correct string syntax in C

    Strings must be in double quotes, so "File error" is correct.
  2. Step 2: Validate perror usage

    perror takes a const char* string, so perror("File error") is correct syntax.
  3. Final Answer:

    perror("File error"); -> Option C
  4. Quick Check:

    Use double quotes for strings in perror [OK]
Hint: Use double quotes for strings in perror [OK]
Common Mistakes:
  • Using single quotes instead of double quotes
  • Passing variable names without quotes
  • Missing parentheses in function call
3. What will be the output of the following C code snippet if 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;
}
medium
A. Error: strerror function not found
B. Error: Permission denied
C. Error: Unknown error
D. Error: No such file or directory

Solution

  1. Step 1: Understand strerror(errno)

    strerror returns a string describing the error number stored in errno.
  2. Step 2: Match errno value to message

    ENOENT means "No such file or directory", so strerror(errno) returns that message.
  3. Final Answer:

    Error: No such file or directory -> Option D
  4. Quick Check:

    strerror(ENOENT) = "No such file or directory" [OK]
Hint: strerror(errno) shows error message for current error [OK]
Common Mistakes:
  • Confusing error messages for different errno values
  • Assuming strerror prints instead of returns string
  • Not including header
4. Identify the error in this code snippet that tries to print an error message:
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main() {
    int errnum = ENOENT;
    printf("Error: %s\n", perror(errnum));
    return 0;
}
medium
A. Using perror inside printf is incorrect; perror prints directly and returns void
B. Missing header
C. Incorrect error number assigned to errnum
D. printf format specifier %s should be %d

Solution

  1. Step 1: Check perror return type

    perror prints directly to stderr and returns void; it does not return a string.
  2. Step 2: Analyze usage inside printf

    Passing perror(errnum) inside printf is invalid because perror returns void, not a string.
  3. Final Answer:

    Using perror inside printf is incorrect; perror prints directly and returns void -> Option A
  4. Quick Check:

    perror prints itself, don't use inside printf [OK]
Hint: perror prints error; don't use inside printf [OK]
Common Mistakes:
  • Using perror inside printf expecting a string
  • Confusing perror and strerror return types
  • Ignoring compiler warnings about void return
5. You want to write a function that opens a file and prints a clear error message if it fails. Which approach correctly uses 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;
}
hard
A. Call perror(filename) without message, then return NULL
B. Call perror("Failed to open file") to print error, then return NULL
C. Print printf("Error: %s\n", strerror(errno)) only, then return NULL
D. Return NULL without printing any error message

Solution

  1. Step 1: Understand error reporting with perror

    perror prints your message plus the system error, making it clear why fopen failed.
  2. Step 2: Compare options for clarity

    Call perror("Failed to open file") to print error, then return NULL uses perror("Failed to open file"), which is clear and standard. Print printf("Error: %s\n", strerror(errno)) only, then return NULL prints only strerror message without context. Call perror(filename) without message, then return NULL prints filename only, which is less clear. Return NULL without printing any error message prints nothing.
  3. Final Answer:

    Call perror("Failed to open file") to print error, then return NULL -> Option B
  4. Quick Check:

    Use perror with custom message for clear errors [OK]
Hint: Use perror with custom text for clear error messages [OK]
Common Mistakes:
  • Printing strerror without context message
  • Using perror with filename only, confusing output
  • Not printing any error message on failure
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard