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);
}- A. 1 1 1
- B. 1 2 3
- C. 0 1 2
- 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
- What is printed? c int a[5] = {1, 2}; printf("%d", a[3]);
- What is printed? c int x = 5; printf("%d", x > 3 ? x < 10 ? 1 : 2 : 3);
- What is printed? c int i; for (i = 0; i < 10; i += 3) printf("%d ", i);
- What is printed? c int x = 10; int p = &x; int q = &p; q = 20; printf("%d", x);
- What is printed? c char s[20] = "Code"; strcat(s, "Hub"); printf("%zu", strlen(s));
- What is printed on a two's-complement machine? c printf("%d", ~5);