Challenge - 5 Problems
Errno Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
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.
✗ Incorrect
When fopen fails to open a file that does not exist, errno is set to 2, which means 'No such file or directory'.
🧠 Conceptual
intermediate1:30remaining
What does errno represent in C?
Choose the best description of what errno is used for in C programming.
Attempts:
2 left
💡 Hint
Think about how C functions communicate errors without exceptions.
✗ Incorrect
errno is a global variable that is set by system calls and library functions when an error occurs to indicate the error code.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
errno is only meaningful immediately after a failing function call.
✗ Incorrect
errno is only set when an error occurs. If you do other calls before checking errno, it may be reset or unchanged. Always check errno immediately after the failing call.
📝 Syntax
advanced2: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.
Attempts:
2 left
💡 Hint
Do not redeclare errno as a local variable. Check the function's return value correctly.
✗ Incorrect
Option C correctly includes errno.h, does not redeclare errno, and checks remove() return value properly (non-zero means error).
🚀 Application
expert2: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; }
Attempts:
2 left
💡 Hint
Error messages depend on why fopen fails, which can vary by file and system.
✗ Incorrect
The number of distinct error messages depends on the system state and permissions for each file. It can vary, so the answer depends on the environment.