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); }- A. Compilation error: x redefined
- B. 10, since extern refers to the single x defined in file1.c
- C. 0, since each file gets its own independent x
- 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
- What is printed? c int x = 100; void f(void) { int x = 5; { int x = 1; printf("%d ", x); } printf("%d ", x); } int main(void) { f()…
- What is printed? c int x = 5; int f(void) { int x = x + 1; return x; } int main(void) { printf("%d", f()); }
- What is printed? c int f(void) { static int x; return x++; } int main(void) { printf("%d %d %d", f(), f(), f()); }
- What is printed? c static int helper(int n) { return n 2; } int main(void) { printf("%d", helper(5)); }
- What is printed? c void f(void) { static int x = 0; x += 10; printf("%d ", x); } int main(void) { for (int i = 0; i < 3; i++) f(); }
- What is printed? c int global = 1; void modify(void) { int global = 2; global++; } int main(void) { modify(); printf("%d", global); }