Unsigned wraparound in Turbo C (DOS): what does this program print?\n\n#include<stdio.h>\ntypedef unsigned long int uli;\ntypedef uli u;\n\nint main()\n{\n uli a;\n u b = (u)-1;\n a = (uli)-1;\n printf("%lu, %lu", a, b);\n return 0;\n}

Difficulty: Easy

Correct Answer: 4294967295, 4294967295

Explanation:


Introduction / Context:
This checks knowledge of unsigned wraparound and typical widths under Turbo C. Casting -1 to an unsigned type yields the maximum representable value for that type.


Given Data / Assumptions:

  • In 16-bit Turbo C, unsigned long is 32-bit.
  • printf with %lu expects unsigned long.
  • Both a and b are unsigned long values assigned -1 after casting.


Concept / Approach:
Two's complement and modulo arithmetic: (unsigned long)-1 maps to 2^32 - 1 = 4294967295.


Step-by-Step Solution:
1) a = (unsigned long)-1 ⇒ 4294967295.2) b = (unsigned long)-1 via typedef u ⇒ 4294967295.3) Printing both yields "4294967295, 4294967295".


Verification / Alternative check:
Print sizeof(unsigned long) to confirm 4, then print the values.


Why Other Options Are Wrong:
Options A/B are arbitrary numbers; Option D is incorrect because defined unsigned wraparound is not garbage.


Common Pitfalls:
Using a wrong format specifier (e.g., %u) or assuming 64-bit widths on modern systems; this is specific to Turbo C.


Final Answer:
4294967295, 4294967295

Discussion & Comments

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