ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
void f(void) {
    static int calls = 0;
    int local = 0;
    calls++;
    local++;
    printf("%d %d ", calls, local);
}
int main(void) {
    f();
    f();
    f();
}
  1. A. 1 1 1 1 1 1
  2. B. 1 1 2 1 3 1
  3. C. 3 3 3 3 3 3
  4. D. 1 1 1 2 1 3

Correct answer: B. 1 1 2 1 3 1

calls is static and persists, accumulating across calls (1, then 2, then 3). local is an ordinary automatic variable, recreated fresh at 0 every single call, then incremented once to 1 within that call — it never carries over, always printing 1. Output: 1 1 2 1 3 1.

More C Programming questions

Exams that ask this topic