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);
}
  1. A. Compilation error: x redefined
  2. B. Undefined behavior: two objects both named x
  3. C. 0, the second declaration resets x
  4. 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

Exams that ask this topic