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?
- A.
Data will be written to a random location
- B.
The program will write to standard output
- C.
The program will write to standard output
- 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().