Casting chain and sizeof result in Turbo C (DOS): what is printed here?\n\n#include<stdio.h>\ndouble i;\n\nint main()\n{\n (int)(float)(char) i; // value unused; type chain matters\n printf("%d", sizeof((int)(float)(char)i));\n return 0;\n}

Difficulty: Easy

Correct Answer: 2

Explanation:


Introduction / Context:
The expression casts i through char, then float, then int, and sizeof is applied to the resulting type. In Turbo C (16-bit DOS), int is typically 2 bytes.


Given Data / Assumptions:

  • sizeof(int) = 2 bytes on Turbo C 16-bit.
  • sizeof is computed at compile time based on the expression type after casts.


Concept / Approach:
The final cast determines the sizeof result. Intermediate casts do not matter for sizeof once the final type is known.


Step-by-Step Solution:
1) (char)i yields type char.2) (float)(char)i yields type float.3) (int)(float)(char)i yields type int.4) sizeof(int) on Turbo C is 2 ⇒ the program prints 2.


Verification / Alternative check:
Print sizeof(int) directly to confirm 2 on the target toolchain.


Why Other Options Are Wrong:
Options C/D assume 32-/64-bit sizes not applicable to Turbo C; Option A confuses char size with the final cast.


Common Pitfalls:
Believing that the value of i or intermediate types influence sizeof once the final cast is int.


Final Answer:
2

Discussion & Comments

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