What will happen if the user tries to write to a file that could not be opened…

2025

What will happen if the user tries to write to a file that could not be opened in C?

  1. A.

    Data will be written to a random location

  2. B.

    The program will write to standard output

  3. C.

    The program will write to standard output

  4. D.

    The file pointer will be NULL and should be checked before writing

Attempted by 358 students.

Show answer & explanation

Correct answer: D

In C programming, if a file cannot be opened using functions like fopen(), the function returns NULL. Attempting to write to a file using a NULL file pointer can cause undefined behavior or program crashes.

Therefore, the file pointer should always be checked before performing write operations such as fprintf() or fputs().

Explore the full course: Rssb Senior Computer Instructor