Hi,

I'm trying to close a file using fclose, but the following program seg-faults:


#include <stdio.h>

int main(void)
{
    FILE* fp = fopen("unfounded.txt", "r");

    if(fp == NULL)
        fclose(fp);

    return 0;
}


The file I'm trying to open doesn't exist in the directory, so fp is
indeed NULL. So it's just fclose(NULL).
Is fclose(NULL) legal according to the C standard ? As an end-user I
expect fclose(fp) to close the file (if it is already open) or just do
nothing (if it is not open). So the seg fault -- is this a compiler
bug ?

PS: I hope this is the right place to ask this question.


-- Satish.BD

Reply via email to