ProgrammingVery hardProduct company OA
What is printed?
What is printed?
#include <stdio.h>
int x; // tentative definition, file scope
int x; // another tentative definition, same file
int main(void) {
x = 5;
printf("%d", x);
}- A. Compilation error: x redefined
- B. Undefined behavior: two objects both named x
- C. 0, the second declaration resets x
- D. 5 — multiple tentative definitions of the same file-scope variable (with no initializer) merge into one object in C
Correct answer: D. 5 — multiple tentative definitions of the same file-scope variable (with no initializer) merge into one object in C
C (unlike C++) explicitly permits multiple 'tentative definitions' of a file-scope variable with no initializer in the same translation unit; they all refer to a single object, so writing 5 through x and reading it back works fine.
More C Programming questions
- What is printed? c static int x = 5; void f(void); void f(void) { printf("%d", x); } int main(void) { f(); }
- What is printed? c int a = 2, b = 3, c = 4; int r = a + b c - a / b; printf("%d", r);
- What is printed? c int a = 5; int r = a > 3 ? 10 : 20 + 5; printf("%d", r);
- What is printed? c int a = 4; int r = ~a + 1 == -a; printf("%d", r);
- What is printed? c int a = 3, b = 4, c = 5; int r = a + b == c ? 1 : 0; printf("%d", r);
- What is printed? c int a = 5; int b = a++, c = ++a; printf("%d %d %d", a, b, c);