Home › C Tutorial › C NULL and errors
C NULL and errors
Detect failure.
Many functions return NULL or -1 on failure. Check results before using them. perror and errno describe the problem.
Example code
#include <stdio.h>
int main() {
FILE *f = fopen("missing.txt", "r");
if (f == NULL) { perror("fopen"); return 1; }
fclose(f);
return 0;
}