ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int x = 5;
int getX(void) {
    return x;
}
int main(void) {
    int x = 20;
    printf("%d %d", x, getX());
}
  1. A. 20 20
  2. B. 5 5
  3. C. 20 5
  4. D. 5 20

Correct answer: C. 20 5

main's local x=20 only shadows the global within main's own scope; printf's first argument, x, refers to that local, 20. getX() is a separate function with no access to main's local x at all — it only ever sees the file-scope global x=5, which it returns unchanged.

More C Programming questions

Exams that ask this topic