ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int x = 1;
void incrementGlobal(void) {
    x++;
}
int main(void) {
    int x = 10;
    incrementGlobal();
    printf("%d", x);
}
  1. A. 11
  2. B. 10
  3. C. 2
  4. D. Compile-time error: x is ambiguous

Correct answer: B. 10

incrementGlobal() refers to the file-scope global x (the only x visible from inside that function's own body), incrementing it to 2 — but that is a completely separate variable from main()'s own local x=10, which shadows the global only within main() and is never touched by incrementGlobal(). main() prints its own local x, still 10.

More C Programming questions

Exams that ask this topic