ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int f(int *n) {
if (*n <= 0) return 0;
(*n)--;
return 1 + f(n);
}
int main(void) {
int x = 5;
printf("%d %d", f(&x), x);
}- A. 5 5
- B. 0 5
- C. 5 1
- D. 5 0
Correct answer: D. 5 0
Passing a pointer means every recursive call shares and mutates the SAME x (unlike passing n by value). Each call decrements x by 1 and adds 1 to the count, running 5 times until *n reaches 0, so f returns 5 and x ends at 0.
More C Programming questions
- Assume a 64-bit LP64 ABI with natural alignment (no #pragma pack). What is printed? c struct S { char a; int b; char c; }; printf("%zu"…
- Assume a 64-bit LP64 ABI with natural alignment. What is printed? c struct S { int a; char b; int c; }; printf("%zu", sizeof(struct S));
- Assume a 64-bit LP64 ABI with #pragma pack(1) applied to this struct only. What is printed? c #pragma pack(push, 1) struct Packed { char…
- Assume a 64-bit LP64 ABI. What is printed? c struct A { char x; }; struct B { struct A a; char y; }; struct C { struct B b; char z; }…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; struct Point p1 = {1, 2}; struct Point p2 = p1; p2.x = 99…
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { int data; struct Node next; }; printf("%zu", sizeof(struct Node));