ProgrammingHardTCS NQT · Wipro

What is printed?

What is printed?
#include <stdio.h>
int rev(int n, int acc) {
    return n == 0 ? acc : rev(n / 10, acc * 10 + n % 10);
}
int main(void) {
    printf("%d", rev(1234, 0));
}
  1. A. 1234
  2. B. 4312
  3. C. 3421
  4. D. 4321

Correct answer: D. 4321

The accumulator takes the last digit each time, so the digits come out reversed: 4, then 43, 432 and 4321.

More C Programming questions

Exams that ask this topic