C on 16-bit DOS (Turbo C): what will this program print, and why does the cast chain not matter? #include<stdio.h> double i; int main() { (int)(float)(char) i; // no effect on i or sizeof printf("%d", sizeof(i)); return 0; }

Difficulty: Easy

Correct Answer: 8

Explanation:

Introduction / Context: This question tests understanding of C's sizeof operator and the effect (or lack of effect) of cast expressions that are not used. In classic 16-bit DOS compilers like Turbo C, the size of fundamental types can differ from today’s 32/64-bit compilers. Here, we focus on what actually gets printed and why the cast chain does not change the result.

Given Data / Assumptions:

  • Environment: Turbo C on DOS (16-bit).
  • Global variable: double i;
  • Prints: printf("%d", sizeof(i));
  • There is a cast chain (int)(float)(char) i; whose value is not used.

Concept / Approach: The sizeof operator returns the size, in bytes, of its operand’s type at compile time. Unused cast expressions are evaluated only for their value category/type (and may be optimized away); they do not change the type or size of the declared object. On Turbo C, sizeof(double) is typically 8 bytes.

Step-by-Step Solution: 1) Identify the printed expression: sizeof(i) equals sizeof(double). 2) On Turbo C (16-bit), sizeof(double) is 8. 3) The cast chain forms a temporary value and is discarded; it does not alter i. 4) Therefore, the program prints 8.

Verification / Alternative check: Remove the cast line entirely; the output remains the same because sizeof(i) depends only on i’s type, not on prior unused expressions.

Why Other Options Are Wrong: 4: On Turbo C, double is not 4 bytes.
16/22: These sizes do not match Turbo C's double.
0: sizeof never returns zero for an object type.

Common Pitfalls: Assuming sizeof(double) is always the host’s modern 8/16 bytes; conflating type conversions with object size; believing that an unused cast changes the variable’s type.

Final Answer: 8

More Questions from Complicated Declarations

Discussion & Comments

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