ProgrammingHardWipro · Cognizant pattern
What happens when these two files are linked together?
What happens when these two files are linked together?
/* file1.c */
extern int shared;
void show(void) { printf("%d", shared); }
/* file2.c */
int shared = 99;
int main(void) {
show();
}- A. Prints 99
- B. Linker error: shared is declared but never defined
- C. Prints 0
- D. Compile-time error: extern requires an initializer
Correct answer: A. Prints 99
file1.c's extern int shared; is just a DECLARATION, promising the linker that shared is defined somewhere (with external linkage). file2.c provides that actual definition, int shared = 99;. The linker successfully resolves file1.c's reference to file2.c's definition, so show() correctly reads and prints 99.
More C Programming questions
- What is printed? c void f(void) { static int calls = 0; int local = 0; calls++; local++; printf("%d %d ", calls, local); } int main(void)…
- What is printed? c int x = 5; int getX(void) { return x; } int main(void) { int x = 20; printf("%d %d", x, getX()); }
- What is printed? c static int privateCounter = 0; void bump(void) { privateCounter++; } int getCounter(void) { return privateCounter; }…
- What is printed? c int value = 100; void modify(int value) { value = 200; } int main(void) { modify(value); printf("%d", value); }
- What is printed? c int a = 1; int b = a++ + a++; int c = ++a + ++a; printf("UB UB");
- What is printed? c int a = 5; int b = 10; int result = a > b ? a : b > 0 ? b : 0; printf("%d", result);