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

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

Discussion & Comments

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