Question:

What will be the output of the following program?

#include <stdio.h>
void test() {
 static int x;
  printf("%d", x);
  x++;
}
int main() {
  test();
  test();
  test();
  return 0;
}

Show Hint

A static local keeps its value between calls and starts at 0.
Updated On: Jul 2, 2026
  • 1 2 3
  • 0 1 2
  • 0 1 0
  • 1 1 1
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Look at the static variable. static int x has no explicit initializer, so C sets it to 0. Being static, it keeps its value between calls.

Step 2: First call to test. It prints x which is 0, then x++ makes x become 1.

Step 3: Second call. x is still 1 from before, so it prints 1, then x++ makes x become 2.

Step 4: Third call. x is 2, so it prints 2, then x++ makes x become 3.

Step 5: The printed characters are 0, 1, 2 with no spaces added, giving 012. The answer is 0 1 2 (option B).
Was this answer helpful?
0
0