ProgrammingVery hardWipro · Accenture · Capgemini pattern
What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs aligned to their widest member with no packing pragma)? #include <stdio.h> int count = 0; void rec(int n) { if (n <= 0) return; count++; rec(n / 2); } int main(void) { rec(100); printf("%d", count); return 0; }
- A. 50
- B. 6
- C. 100
- D. 7
Correct answer: D. 7
n halves each call (integer division): 100,50,25,12,6,3,1, then 0 stops it; count is incremented once per call before the n==0 base case, i.e. 7 times.
More C Programming questions
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…