ProgrammingHardAccenture · Capgemini

What is printed?

What is printed?
int next(void) { static int n; return n += 2; }
int prev(void) { static int n = 10; return n -= 3; }
int main(void) {
    int a = next(), b = prev();
    int c = next(), d = prev();
    printf("%d %d %d %d", a, b, c, d);
}
  1. A. 2 7 4 1
  2. B. 0 10 2 7
  3. C. 2 7 4 4
  4. D. 2 7 2 7

Correct answer: C. 2 7 4 4

Each function has its own static n; the same name does not make them share. next's n starts at 0 and goes 2, 4; prev's starts at 10 and goes 7, 4. Statics persist between calls, so '2 7 2 7' is wrong.

More C Programming questions

Exams that ask this topic