In C (16-bit Turbo C under DOS), what will this program output, considering 'extern' without a definition and assignment before any definition? #include<stdio.h> int main() { extern int i; // declaration only, no definition here i = 20; // assignment requires a definition to exist at link time printf("%d ", sizeof(i)); // sizeof uses the type, not the object return 0; }

Difficulty: Medium

Correct Answer: Linker Error : Undefined symbol 'i'

Explanation:


Introduction / Context:
This question examines how C handles external declarations, object definitions, and the difference between compile-time type knowledge and link-time symbol resolution, especially on a 16-bit Turbo C platform. It also distinguishes what sizeof needs (type information) versus what an assignment needs (a real, defined object).



Given Data / Assumptions:

  • Compiler/Platform: Turbo C (16-bit DOS).
  • Code declares extern int i; but does not provide a matching definition in the translation unit or any linked object.
  • The program assigns i = 20; and prints sizeof(i).


Concept / Approach:
In C, extern int i; is a declaration that promises a definition of i exists somewhere else (another file/object). The compiler can compile references using only the type, but the linker must later find the actual storage (the definition). sizeof(i) needs only type information, so compilation is fine. However, performing an assignment to i requires that the linker resolve the symbol to real storage; if no definition is present, the linker emits an undefined symbol error.



Step-by-Step Solution:
The declaration supplies type: int.The assignment i = 20; generates a reference to a definition.The linker searches for a definition (e.g., int i;) but none is provided.Therefore, link fails with “Undefined symbol ’i’”.



Verification / Alternative check:
If you add int i; globally in the same file or an object file, the program links and runs, and on Turbo C sizeof(int) is 2, so output would be 2. Without the definition, it fails at link stage.



Why Other Options Are Wrong:
A: “2” is what would print if a definition existed. B: “4” is the typical 32-bit int size, not Turbo C. C: Behavior does not legitimately vary; the link fails. E: There is no standard “warning then run” path; undefined external is a link error.



Common Pitfalls:
Assuming extern allocates storage (it does not); thinking sizeof implies definition exists; forgetting link-time resolution.



Final Answer:
Linker Error : Undefined symbol 'i'

More Questions from Declarations and Initializations

Discussion & Comments

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