ProgrammingHardTCS NQT · Infosys · Product Company SDE

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 rec(int n, int acc) { if (n == 0) return acc; return rec(n - 1, acc + n); } int main(void) { printf("%d", rec(5, 0)); return 0; }

  1. A. 15
  2. B. 5
  3. C. 10
  4. D. 120

Correct answer: A. 15

This accumulator-style recursion adds n down to 1 into acc: 5+4+3+2+1=15.

More C Programming questions

Exams that ask this topic