Error Handling with errno
C has no exceptions. Functions return error codes or NULL and set errno. strerror and perror turn it into a message.
Example
#include <errno.h>
FILE *f = fopen("/nonexistent/file.txt", "r");
if (!f) {
printf("failed: %s\n", strerror(errno));
printf("%d\n", errno == ENOENT);
}Result
failed: No such file or directory 1
Exercise
Message for an error code.
(errno)Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.