0
0
Cprogramming~20 mins

Using errno in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Errno Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code using errno after fopen fails?
Consider the following C code snippet that tries to open a non-existent file. What will it print?
C
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    FILE *fp = fopen("nonexistent.txt", "r");
    if (!fp) {
        printf("Error code: %d\n", errno);
        printf("Error message: %s\n", strerror(errno));
    }
    return 0;
}
A
Error code: 0
Error message: Success
B
Error code: 2
Error message: No such file or directory
C
Error code: 13
Error message: Permission denied
D
Error code: 22
Error message: Invalid argument
Attempts:
2 left
💡 Hint
errno is set by system calls and library functions when an error occurs. fopen returns NULL on failure and sets errno.
🧠 Conceptual
intermediate
1:30remaining
What does errno represent in C?
Choose the best description of what errno is used for in C programming.
AA global variable set by system calls and library functions to indicate the error code of the last error.
BA function that returns the last error message as a string.
CA constant integer representing the number of errors encountered since program start.
DA macro that clears all errors in the program.
Attempts:
2 left
💡 Hint
Think about how C functions communicate errors without exceptions.
🔧 Debug
advanced
2:00remaining
Why does this code print 'Error code: 0' even after fopen fails?
Examine the code below. It tries to open a file that does not exist but prints 'Error code: 0'. What is the cause?
C
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    errno = 0;
    FILE *fp = fopen("missing.txt", "r");
    if (!fp) {
        printf("Error code: %d\n", errno);
        printf("Error message: %s\n", strerror(errno));
    }
    return 0;
}
Aerrno must be checked immediately after fopen; some functions reset errno if not checked quickly.
Berrno was set to 0 before fopen, but fopen did not set errno on failure in this environment.
Cfopen succeeded, so errno remains 0.
DThe program forgot to include <errno.h>, so errno is undefined.
Attempts:
2 left
💡 Hint
errno is only meaningful immediately after a failing function call.
📝 Syntax
advanced
2:00remaining
Which option correctly declares and uses errno to check an error after a system call?
Select the code snippet that correctly uses errno to detect an error after calling a system function.
A
#include &lt;errno.h&gt;
#include &lt;stdio.h&gt;

int main() {
    int errno = 0;
    int result = remove("file.txt");
    if (result == -1) {
        printf("Error: %s\n", strerror(errno));
    }
    return 0;
}
B
#include &lt;stdio.h&gt;

int main() {
    int result = remove("file.txt");
    if (result != 0) {
        printf("Error: %s\n", strerror(errno));
    }
    return 0;
}
C
#include &lt;errno.h&gt;
#include &lt;stdio.h&gt;

int main() {
    int result = remove("file.txt");
    if (result != 0) {
        printf("Error: %s\n", strerror(errno));
    }
    return 0;
}
D
#include &lt;errno.h&gt;
#include &lt;stdio.h&gt;

int main() {
    int result = remove("file.txt");
    if (result == 0) {
        printf("Error: %s\n", strerror(errno));
    }
    return 0;
}
Attempts:
2 left
💡 Hint
Do not redeclare errno as a local variable. Check the function's return value correctly.
🚀 Application
expert
2:30remaining
How many distinct error messages can this code print when opening files?
Given the code below, how many different error messages can it print if the file opening fails?
C
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    const char *files[] = {"file1.txt", "file2.txt", "file3.txt"};
    for (int i = 0; i < 3; i++) {
        FILE *fp = fopen(files[i], "r");
        if (!fp) {
            printf("File: %s, Error: %s\n", files[i], strerror(errno));
        } else {
            fclose(fp);
        }
    }
    return 0;
}
A0
B1
C3
DDepends on the system and file permissions
Attempts:
2 left
💡 Hint
Error messages depend on why fopen fails, which can vary by file and system.