ProgrammingModerateAccenture · Capgemini pattern

What is printed?

What is printed?
int x = 5;
void f(void) { int x = 1; x++; }
void g(void) { x++; }
int main(void) {
    f(); g(); g();
    printf("%d", x);
}
  1. A. 8
  2. B. 4
  3. C. 2
  4. D. 7

Correct answer: D. 7

Inside f the local x shadows the global, so f's increment dies with its local copy; the two g calls raise the global from 5 to 7. 8 assumes f also touched the global, and 4 treats int x = 1 in f as an assignment to the global.

More C Programming questions

Exams that ask this topic