ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int f(void) {
    static int a[3];
    a[0]++;
    return a[0];
}
int main(void) {
    int x = f();
    int y = f();
    int z = f();
    printf("%d %d %d", x, y, z);
}
  1. A. 3 3 3
  2. B. 1 2 3
  3. C. 1 1 1
  4. D. 0 1 2

Correct answer: B. 1 2 3

The static array is zero-initialised once and a[0] increments on each call, so the three calls return 1, 2 and 3.

More C Programming questions

Exams that ask this topic