Complete the code to print an error message using perror.
#include <stdio.h> #include <errno.h> int main() { FILE *fp = fopen("nonexistent.txt", "r"); if (fp == NULL) { [1]("File open error"); } return 0; }
The perror function prints a descriptive error message to stderr based on the current errno value.
Complete the code to get the error message string from errno using strerror.
#include <stdio.h> #include <string.h> #include <errno.h> int main() { int errnum = 2; printf("Error message: %s\n", [1](errnum)); return 0; }
The strerror function returns a pointer to the error message string corresponding to the error number.
Fix the error in the code to correctly print the error message using perror.
#include <stdio.h> #include <errno.h> int main() { FILE *fp = fopen("missing.txt", "r"); if (fp == NULL) { perror([1]); } return 0; }
The perror function expects a string prefix to print before the error message. Passing a string literal like "File error" is correct.
Fill both blanks to create a dictionary-like mapping of error numbers to messages using strerror.
#include <stdio.h> #include <string.h> #include <errno.h> int main() { int errors[] = {1, 2, 3}; for (int i = 0; i < 3; i++) { printf("Error %d: %s\n", errors[i], [1](errors[i])); } return [2]; }
Use strerror to get the error message string for each error number. Return 0 to indicate successful program completion.
Fill all three blanks to print an error message using strerror and return an error code.
#include <stdio.h> #include <string.h> #include <errno.h> int main() { FILE *fp = fopen("file.txt", "r"); if (fp == NULL) { fprintf(stderr, "Error opening file: %s\n", [1]([2])); return [3]; } fclose(fp); return 0; }
strerror(errno) returns the error message string for the last error. Returning 1 indicates an error exit code.