ProgrammingVery hardGATE CS · Product company OA
A single C source file contains these three lines at file scope. What happens?
A single C source file contains these three lines at file scope. What happens?
int x; int x; int x = 5;
- A. It compiles, but the linker reports multiple definitions of x
- B. Compilation error: x is defined three times
- C. It compiles, but x is 0 because the first definition wins
- D. It compiles: the first two are tentative definitions of the same x, which is finally defined as 5
Correct answer: D. It compiles: the first two are tentative definitions of the same x, which is finally defined as 5
At file scope, a declaration with no initialiser and no storage class is a tentative definition. Several may appear in one file, and together with the single real definition they all refer to one object, initialised to 5. Two initialisers would be an error.
More C Programming questions
- Assume 4-byte int, 8-byte double and natural alignment. What is printed? c struct T { double d; int i; }; struct T arr[3]; printf("%td"…
- What is printed? c int len(const char s) { return s ? 1 + len(s + 1) : 0; } int cnt(const char s, char c) { return !s ? 0 : (s == c) +…
- What is printed? c int dr(int n) { return n < 10 ? n : dr(n / 10 + n % 10); } int main(void) { printf("%d %d", dr(9875), dr(99999)); }
- What is printed? c unsigned x = 0xF0F0; x = x - ((x >> 1) & 0x5555); x = (x & 0x3333) + ((x >> 2) & 0x3333); x = (x + (x >> 4)) & 0x0F0F…
- What is printed? c struct N { int v, next; } t[] = {{10, 2}, {20, -1}, {30, 4}, {40, 1}, {50, 1}}; int s = 0; for (int i = 0; i != -1; i =…
- Given int x; const int pc; int p; int const cp = &x;, which assignment is a constraint violation that the compiler must diagnose?