ProgrammingHardProduct company OA · GATE CS

file1.c contains `static int count = 5;` at file scope. file2.c contains `extern int count;` and uses count in main. Nothing else defines count. What happens when both files are compiled and linked together?

  1. A. file1.c fails to compile because a static global cannot be initialised
  2. B. The program links and file2 sees count as 0
  3. C. Each file compiles, but linking fails with an undefined reference to count
  4. D. The program links and file2 sees the value 5

Correct answer: C. Each file compiles, but linking fails with an undefined reference to count

static at file scope gives count internal linkage: it is invisible outside file1.c. file2's extern declaration therefore refers to an external count that nobody defines, so the linker reports an undefined reference. An extern declaration never creates storage.

More C Programming questions

Exams that ask this topic