ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
// header.h (included by both files)
extern int sharedValue;
// a.c
#include "header.h"
int sharedValue = 42;
// b.c (linked with a.c)
#include "header.h"
int main(void) {
    printf("%d", sharedValue);
}
  1. A. Compilation error: sharedValue is declared but never defined
  2. B. 42
  3. C. 0
  4. D. Linker error: multiple definition of sharedValue

Correct answer: B. 42

header.h's `extern int sharedValue;` is just a declaration (no storage allocated), included identically in both files without conflict. a.c alone provides the single actual DEFINITION (`int sharedValue = 42;`); b.c's extern declaration correctly refers to that same external symbol at link time, reading 42.

More C Programming questions

Exams that ask this topic