ProgrammingVery hardGATE CS · Accenture
What is printed? int f(int n) { static int c = 0; c += n; if (n <= 0) return c; int r = f(n - 1); return r + c; } printf("%d", f(3));
- A. 24
- B. 18
- C. 12
- D. 30
Correct answer: A. 24
The static c grows to 3, 5, 6 and 6 on the way down, so f(0) returns 6. Each return then adds the final shared c = 6: f(1) = 12, f(2) = 18, f(3) = 24. Treating c as a per-call local gives the trap values.
More C Programming questions
- What does this C program print? unsigned int x = 0xB5; printf("%x", x ^ (x >> 1));
- What is the value of s after this loop? int i, s = 0; for (i = 0; i < 5; i++) { switch (i % 3) { case 0: s += 1; case 1: s += 10; break…
- What is printed? c #include <stdio.h> int main(void) { int a = 0, b = 1, c = 2; int r = (a++ && b++) || (c-- && a--); printf("%d %d %d…
- What is printed? Assume int occupies 4 bytes. c #include <stdio.h> int main(void) { int i = 5; sizet n = sizeof(i++) + sizeof(i = 100)…
- What is printed? Assume int is 32 bits and unsigned char is 8 bits. c #include <stdio.h> int main(void) { unsigned char c = 0x0F…
- What is printed? Assume int and unsigned int are 32 bits. c #include <stdio.h> int main(void) { int a = -1; unsigned int b = 1; printf("%d…