ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int f(int n) {
if (n == 0) return 0;
int r = f(n - 1);
printf("%d ", n);
return r;
}
int main(void) { int x = f(4); printf("| %d", x); }- A. 4 3 2 1 | 0
- B. 1 2 3 4 | 4
- C. 4 3 2 1 | 4
- D. 1 2 3 4 | 0
Correct answer: D. 1 2 3 4 | 0
Each frame recurses fully before printing its own n, so prints happen in increasing order as the stack unwinds from the deepest call back out: 1 2 3 4. The return value r is always whatever f(0) returned, which is 0, propagated unchanged through every frame.
More C Programming questions
- What is printed? c union U { char c; int i; double d; }; printf("%zu", sizeof(union U));
- Assume a 64-bit LP64 ABI. What is printed? c struct Inner { char c; int i; }; struct Outer { int x; struct Inner in; double d; }…
- On a typical x86-64 GCC/Clang target, what is printed? c struct F { unsigned a : 20; unsigned b : 20; }; printf("%zu", sizeof(struct F));
- Assume a 64-bit LP64 ABI. What is printed? c struct S { char c; int arr[2]; }; printf("%zu %zu", sizeof(struct S), offsetof(struct S, arr));
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { int val; struct Node next; }; printf("%zu", sizeof(struct Node));
- What is printed? c #pragma pack(push, 1) struct Packed { char c; int i; }; #pragma pack(pop) printf("%zu", sizeof(struct Packed));