Difficulty: Medium
Correct Answer: Hello
Explanation:
Introduction / Context:
This code demonstrates a common pointer-copy idiom with a twist: it both assigns in the while condition and repeats an assignment in the loop body with post-increments. Despite seeming redundant, the resulting contents of str2 after the loop are important to determine correctly.
Given Data / Assumptions:
Concept / Approach:
On each iteration, the condition copies the 'current' byte; the body then copies the 'next' byte and advances. When the null terminator is encountered by the condition assignment, the loop exits and the string in str2 is correctly terminated. Despite copying certain bytes twice, the final effect is a normal copy of 'Hello' into str2.
Step-by-Step Solution:
Verification / Alternative check:
Printing str2 shows 'Hello'. Rewriting the loop in the canonical form while ((*t++ = *s++) != '\0'); produces the same final string.
Why Other Options Are Wrong:
Common Pitfalls:
Confusing the immediate console output with the final memory content; assuming the double assignment corrupts the copy (it does not here).
Final Answer:
Hello.
Discussion & Comments