C typedefs and shadowing in Turbo C: what does this program print?\n\n#include<stdio.h>\ntypedef void v;\ntypedef int i;\n\nint main()\n{\n v fun(i, i);\n fun(2, 3);\n return 0;\n}\n\nv fun(i a, i b)\n{\n i s = 2; // s is int\n float i; // here, i is a variable of type float (shadows typedef name)\n printf("%d,", sizeof(i));\n printf(" %d", abs);\n}

Difficulty: Medium

Correct Answer: 4, 12

Explanation:


Introduction / Context:
This question checks understanding of typedef names versus identifiers and how a local variable can shadow a typedef name, affecting sizeof and evaluation of expressions in Turbo C.


Given Data / Assumptions:

  • In Turbo C: sizeof(int) = 2, sizeof(float) = 4 (typical 16-bit).
  • typedef i = int; typedef v = void.
  • Local declaration float i; introduces a variable named i of type float, shadowing the typedef name i.


Concept / Approach:
sizeof(i) refers to the variable i (type float), not the typedef. The arithmetic abs uses ints: a = 2, b = 3, s = 2, so the product is 12.


Step-by-Step Solution:
1) Shadowing: within fun, float i; means i is a variable, so sizeof(i) = sizeof(float) = 4.2) Compute abs = 232 = 12.3) Output format prints "4, 12".


Verification / Alternative check:
Rename the variable (e.g., float f;) and observe sizeof now prints 2 if you use sizeof(i) where i is the typedef int.


Why Other Options Are Wrong:
Options A/B/C assume incorrect sizes or miscompute the product.


Common Pitfalls:
Confusing typedef names with variables; sizeof applied to an identifier uses the identifier's current binding in scope.


Final Answer:
4, 12

More Questions from Complicated Declarations

Discussion & Comments

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