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("\n"); return 0; }

Difficulty: Easy

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 int is assumed to be 16 bits (0..65535).
  • 65536 is exactly 2^16, so it wraps to 0 on assignment in a 16-bit unsigned int.
  • The while loop executes only if i != 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 "\n".


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.

More Questions from Control Instructions

Discussion & Comments

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