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?
- A. file1.c fails to compile because a static global cannot be initialised
- B. The program links and file2 sees count as 0
- C. Each file compiles, but linking fails with an undefined reference to count
- 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
- What is printed? c int a[] = {10, 20, 30, 40, 50}; int p = (int )(&a + 1); int q = &a[1] + 1; printf("%d %d %td", (p - 1), (q - 1), p - q);
- What is printed? c int a = 0, b = 1, c = 2; if (a++ || b-- && ++c) c += 10; printf("%d %d %d", a, b, c);
- What is the output? c int i = 0, s = 0; do { i++; if (i % 2) continue; s += i; } while (i < 7); printf("%d %d", i, s);
- What is printed? c char label(int n) { static char buf[8]; buf[0] = 'A' + n; buf[1] = '0'; return buf; } int main(void) { char a =…
- What is printed? c int i, s = 0; for (i = 1; i < 50; i = i 2 + 1) s += i % 3 ? i : -i; printf("%d %d", s, i);
- Assume ASCII. What is printed? c char s[] = "aB3"; for (char p = s; p; p++) p ^= 32; printf("%d %d %d", s[0], s[1], s[2]);