C (16-bit unsigned int assumed): what does this program print? #include<stdio.h> int main() { unsigned int i = 65536; /* Assume 2 byte integer */ while (i != 0) printf("%d", ++i); printf(" "); return 0; }
-
AInfinite loop
-
B0 1 2 ... 65535
-
C0 1 2 ... 32767 - 32766 -32765 -1 0
-
DNo output
-
ECompilation error
Answer
Correct Answer: No output
Explanation
Introduction / Context:This question targets integer width assumptions and initialization effects. Assigning a value outside the range of a 16-bit unsigned type causes wraparound, which directly determines whether the loop body executes at all.
Given Data / Assumptions:
unsigned intis assumed to be 16 bits (0..65535).65536is exactly 2^16, so it wraps to 0 on assignment in a 16-bitunsigned int.- The
whileloop executes only ifi != 0.
Concept / Approach:On a 16-bit model, unsigned int i = 65536; stores 0 because 65536 mod 65536 = 0. Therefore the loop condition (i != 0) is false at the first test, and the loop body is skipped entirely. Only the trailing newline is printed, which most judges interpret as “no visible output.”
Step-by-Step Solution:
Assignment: i becomes 0 after wrap.Condition:i != 0 → false → loop not entered.Program prints only "".Verification / Alternative check:Change the initializer to 65535 and observe the loop execute; or print the value of i immediately after initialization to confirm it is 0.
Why Other Options Are Wrong:
Infinite loop / sequences of numbers: require the loop to start; here it never starts.Compilation error: standard-compliant code.Common Pitfalls:Forgetting the target width of unsigned int on 16-bit assumptions; assuming 32-bit widths everywhere.
Final Answer:No output.