ProgrammingVery hardGATE CS
What is printed?
What is printed?
#include <stdio.h>
int f(int n) {
static int c = 0;
c++;
if (n >= 2) {
f(n - 1);
f(n - 2);
}
return c;
}
int main(void) {
f(6);
printf("%d", f(0));
return 0;
}- A. 25
- B. 13
- C. 26
- D. 24
Correct answer: C. 26
The static counter c survives all calls. Calls(n) = 1 + Calls(n-1) + Calls(n-2) with Calls(0) = Calls(1) = 1, so Calls(6) = 25. The later f(0) is call number 26 and returns c = 26.
More C Programming questions
- What is printed? Assume pointers are 8 bytes and the string literal's characters are those between the quotes. c #include <stdio.h>…
- What is the exact output? c #include <stdio.h> int main(void) { int n = printf("%d%d", 12, 345); printf("|%d", n); return 0; }
- What is printed? Assume int is 4 bytes and pointers are 8 bytes. c #include <stdio.h> void f(int a[10]) { printf("%zu ", sizeof a); } int…
- What is printed? Assume the usual ABI: char 1 byte, short 2, int 4, with each member aligned to its own size and the struct padded to a…
- What is printed? c #include <stdio.h> int main(void) { char s[] = "abcd"; char p = s; (p)++; p++ += 2; ++p; p[1]--; printf("%s", s)…
- What is printed? Assume C99 or later (division truncates towards zero). c #include <stdio.h> int main(void) { printf("%d %d %d %d", -17 /…