ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int f(int n) {
static int calls = 0;
calls++;
if (n <= 0) return calls;
int r = f(n - 2);
return r * 10 + calls;
}
int main(void) {
printf("%d", f(4));
}- A. 212
- B. 333
- C. 101
- D. 4441
Correct answer: B. 333
The static counter is shared by all calls, so the recursive call increments it before the outer call reads it. Storing the recursive result before reading calls keeps the order defined.
More C Programming questions
- What is printed? c #include <stdio.h> struct P { int v; }; int main(void) { struct P arr[3] = {{1}, {2}, {3}}; int sum = 0; for (struct P…
- What is printed? c #include <stdio.h> int main(void) { char c = 'a'; printf("%d %d", c + 1 == 'b', '9' - '0'); }
- What is printed? c #include <stdio.h> unsigned fibcalls = 0; int fib(int n) { fibcalls++; if (n < 2) return n; return fib(n - 1) + fib(n…
- What is printed for the sorted array below? c #include <stdio.h> int main(void) { int a[] = {1, 3, 3, 3, 5, 8}; int n = 6, lo = 0, hi = n…
- What is printed? c #include <stdio.h> int find(const char hay, const char nd) { for (const char p = hay; p; p++) { const char a = p, b =…
- What is printed? Assume int is 32 bits. c #include <stdio.h> int main(void) { unsigned a = 10; int b = -3; printf("%u", a % b); }