1. Home
  2. Computer Science MCQs
  3. C Programming
  4. A stream is closed with fclose(fp) and the program later…

Added 30 Nov -0001

A stream is closed with fclose(fp) and the program later calls fgetc(fp). What is the status of that second call?

  1. Undefined behaviour, because fp is an indeterminate pointer after fclose Correct answer
  2. It returns EOF, since the stream remembers that it has been closed
  3. It is legal and reopens the file automatically in read mode for you
  4. It returns EOF only if the stream had reached end-of-file before closing

Correct answer

A. Undefined behaviour, because fp is an indeterminate pointer after fclose

17 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs