ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
void f(void) {
    static int x = 0;
    x += 10;
    printf("%d ", x);
}
int main(void) {
    for (int i = 0; i < 3; i++) f();
}
  1. A. 10 10 10
  2. B. 0 10 20
  3. C. 30 30 30
  4. D. 10 20 30

Correct answer: D. 10 20 30

static x is initialized to 0 only once; each call adds 10 and the value persists between calls, producing 10, then 20, then 30 across the three loop iterations.

More C Programming questions

Exams that ask this topic