ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int f(void) {
    static int s = 5;
    return s++;
}
int main(void) {
    int a = f(), b = f(), c = f();
    printf("%d %d %d", a, b, c);
}
  1. A. 7 6 5
  2. B. 5 5 5
  3. C. 4 5 6
  4. D. 5 6 7

Correct answer: D. 5 6 7

The static variable is initialised once to 5, and each call returns the current value and then increments it. Separate statements fix the order of the three calls, so the values are 5, 6 and 7.

More C Programming questions

Exams that ask this topic