Complete the code to include the header needed to use errno.
#include <stdio.h> #include [1] int main() { return 0; }
The <errno.h> header defines the errno variable and error codes.
Complete the code to reset errno before a function call.
#include <stdio.h> #include <errno.h> int main() { errno = [1]; return 0; }
Setting errno to 0 clears previous errors before a new function call.
Fix the error in checking if fopen failed by using errno.
#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; }
ENOENT means the file or directory does not exist, which is common when fopen fails.
Fill both blanks to print a custom error message with errno and its description.
#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; }
The %i format prints an integer (errno), and strerror(errno) returns the error message string.
Fill all three blanks to check errno after a failed open and print a message.
#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; }
EACCES means permission denied. Use %i to print errno and strerror(errno) for the message.
