A stream is closed with fclose(fp) and the program later calls fgetc(fp). What is the status of that second call?
Correct answer
A. Undefined behaviour, because fp is an indeterminate pointer after fclose
17 views
A. Undefined behaviour, because fp is an indeterminate pointer after fclose
17 views
After fclose the FILE object has been released, so the value of the pointer is indeterminate and passing it to any stdio function is undefined behaviour. The call may appear to work, return EOF or crash, depending on what the freed structure now holds. Setting the pointer to NULL after closing does not repair the pointer, but it makes a later mistaken use detectable.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.