C Programming · Question 8 · Medium

What will be the output of the following C program?

#include <stdio.h>

void f(void) {
    static int c = 0;
    c += 2;
    printf("%d ", c);
}

int main(void) {
    f();
    f();
    f();
    return 0;
}
  1. 2 2 2
  2. 2 4 6
  3. 0 2 4
  4. 0 0 0
Answer and explanation

Answer: B. 2 4 6

Explanation: A static local variable is initialised only once and keeps its value between calls, so c becomes 2, then 4, then 6.

Previous: What will be the output of the following C program? · Next: What will be the output of the following C program?

Practise c programming

Try a fresh set of c programming questions with an explanation after every answer, free and without signing up: C Programming practice. Or sit a full placement mock test.

More c programming questions

More technical & programming practice

Java · Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge