ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
// file1.c
int x = 10;
// file2.c (same program, separate translation unit)
extern int x;
void show(void) { printf("%d", x); }
  1. A. Compilation error: x redefined
  2. B. 10, since extern refers to the single x defined in file1.c
  3. C. 0, since each file gets its own independent x
  4. D. Undefined behavior: x is ambiguous across files

Correct answer: B. 10, since extern refers to the single x defined in file1.c

extern int x; in file2.c is only a declaration (no storage), telling the compiler that x is defined elsewhere; the linker resolves it to the single definition in file1.c, so both files share the same x = 10.

More C Programming questions

Exams that ask this topic