Difficulty: Easy
Correct Answer: This is CuriousTab
Explanation:
Introduction / Context:
This question focuses on correct EOF-controlled reading and format-string behavior in printf. The loop prints each character from the file until EOF; the final printf prints a newline regardless of the unused extra argument.
Given Data / Assumptions:
Concept / Approach:
In while ((ch = getc(fp)) != EOF)
, getc returns an int. The assignment to ch then converts the lower 8 bits, but the comparison uses the promoted int from the assignment expression, which holds the original getc result. As long as the character read is not EOF, the body prints it. After the loop, fclose closes the stream. The final printf("\n", ch)
ignores the superfluous argument and prints only a newline.
Step-by-Step Solution:
Read and echo characters → outputs "This is CuriousTab " including the trailing space.Close file → emit a newline.Net visible line → "This is CuriousTab" (trailing space not visible at the end in the option wording).
Verification / Alternative check:
Replace getc with fgetc and store into an int to avoid any subtlety with unsigned char; output is the same.
Why Other Options Are Wrong:
It is not an infinite loop and there is no error. Truncating at "This is" would require an early stop condition that does not exist here.
Common Pitfalls:
Misusing printf format strings (unused arguments are ignored) and misunderstanding the role of EOF and int return types for character input.
Final Answer:
This is CuriousTab
Discussion & Comments