QP School

Full Version: Not checking return values in C
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Neglecting to check the return values of functions that can fail (e.g., malloc, fopen, etc.) can lead to errors.

// Incorrect
FILE *file = fopen("non_existent_file.txt", "r");
// No check for the success of fopen()

// Correct
FILE *file = fopen("non_existent_file.txt", "r");
if (file == NULL) {
    // Handle error (file not opened)
}