C file I/O API correctness: In the given program with three opened files, what happens when calling fclose(fp, fs, ft);?

Difficulty: Easy

Correct Answer: Error in fclose()

Explanation:


Introduction / Context:
Knowing the exact function signatures in the C standard library prevents subtle API misuse. The fclose function is designed to close a single stream at a time. Passing multiple arguments violates its prototype and leads to a compile-time error, which is important when managing several files in one function.


Given Data / Assumptions:

  • Three FILE* streams are successfully opened: fp, fs, and ft.
  • The code calls fclose(fp, fs, ft); with three arguments.
  • Standard C header is used.


Concept / Approach:

  • The prototype is int fclose(FILE stream); It accepts exactly one FILE.
  • To close multiple files, invoke fclose separately for each pointer.
  • Compilers type-check function calls and will reject calls with the wrong number or types of arguments.


Step-by-Step Solution:

Compare the call fclose(fp, fs, ft) with the prototype fclose(FILE*).Since three arguments are supplied instead of one, this is a mismatched call.Therefore, the result is a compile-time error, not the closing of any files.


Verification / Alternative check:

Correct usage should be: fclose(fp); fclose(fs); fclose(ft); Each call closes a single stream and returns 0 on success.


Why Other Options Are Wrong:

  • Listing any subset of files as closed implies fclose can take multiple pointers, which it cannot.
  • Suggesting only "A.C" closes is arbitrary and still contradicts the function prototype.


Common Pitfalls:

  • Attempting to pass multiple resources to a single-argument API; instead, loop over the list and close individually.
  • Ignoring fclose return values; always check for errors when closing streams.


Final Answer:

Error in fclose()

More Questions from Input / Output

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion