Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to include the header needed to use errno.
C
#include <stdio.h> #include [1] int main() { return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Including instead of
Forgetting to include any header for errno
✗ Incorrect
The <errno.h> header defines the errno variable and error codes.
2fill in blank
mediumComplete the code to reset errno before a function call.
C
#include <stdio.h> #include <errno.h> int main() { errno = [1]; return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting errno to 1 or -1 instead of 0
Using NULL which is for pointers
✗ Incorrect
Setting errno to 0 clears previous errors before a new function call.
3fill in blank
hardFix the error in checking if fopen failed by using errno.
C
#include <stdio.h> #include <errno.h> int main() { FILE *file = fopen("missing.txt", "r"); if (file == NULL) { if (errno == [1]) { perror("File open error"); } } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using EACCES which means permission denied
Using EINVAL which means invalid argument
✗ Incorrect
ENOENT means the file or directory does not exist, which is common when fopen fails.
4fill in blank
hardFill both blanks to print a custom error message with errno and its description.
C
#include <stdio.h> #include <errno.h> #include <string.h> int main() { errno = 0; FILE *f = fopen("nofile.txt", "r"); if (f == NULL) { printf("Error %[1]: %s\n", errno, [2](errno)); } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d with perror (which prints directly)
Using %s with errno (which is an int)
✗ Incorrect
The %i format prints an integer (errno), and strerror(errno) returns the error message string.
5fill in blank
hardFill all three blanks to check errno after a failed open and print a message.
C
#include <stdio.h> #include <errno.h> #include <string.h> int main() { errno = 0; FILE *fp = fopen("data.txt", "r"); if (fp == NULL) { if (errno == [1]) { printf("Permission denied: %[2] - %s\n", errno, [3](errno)); } } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ENOENT instead of EACCES for permission errors
Using %s for errno integer
Using perror instead of strerror in printf
✗ Incorrect
EACCES means permission denied. Use %i to print errno and strerror(errno) for the message.