ProgrammingModerateAccenture · Capgemini

What is printed?

What is printed?
int f(void) { static int n = 5; return n--; }
int main(void) {
    int s = 0;
    while (f() > 2) s++;
    printf("%d %d", s, f());
}
  1. A. 4 1
  2. B. 3 2
  3. C. 2 1
  4. D. 3 1

Correct answer: D. 3 1

The static n keeps its value across calls and f returns the old value. The loop tests 5, 4, 3 (true, s becomes 3) and then 2 (false, n is now 1). The call inside printf returns 1. '3 2' forgets that the failing test also called f.

More C Programming questions

Exams that ask this topic