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);
}- A. Compilation error: sharedValue is declared but never defined
- B. 42
- C. 0
- 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
- What is printed? c int a = 5, b = 2, c; c = a > b ? a++ : --b; printf("%d %d %d", a, b, c);
- What is printed? c int a = 2; int b = (a++, a + 3, a 2); printf("%d %d", a, b);
- What is printed? c int a = 10; int b = a++ + a++; printf("%d", a);
- What is printed? c int x = 5; if (x = 10) printf("true %d", x); else printf("false %d", x);
- What is printed? c int a = 1, b = 0, c = 1; int result = a || b && c; printf("%d", result);
- What is printed? c int x = 5; int y = x++ + (x = 10); printf("%d", y);