ProgrammingModerateWipro · Capgemini pattern

What is printed when count() is called three times in a row?

What is printed when count() is called three times in a row?
void count(void) {
    static int c = 0;
    c++;
    printf("%d ", c);
}
  1. A. 1 1 1
  2. B. 1 2 3
  3. C. 0 1 2
  4. D. 3 3 3

Correct answer: B. 1 2 3

The static local is initialised to 0 only once, before the first call, and keeps its value afterwards, so the calls print 1, 2, 3. Without static every call would print 1.

More C Programming questions

Exams that ask this topic