ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
#include <stdio.h>
void rev(const char *s) {
    if (*s == '\0') return;
    rev(s + 1);
    putchar(*s);
}
int main(void) {
    rev("CAT");
}
  1. A. CAT
  2. B. T
  3. C. TAC
  4. D. CATC

Correct answer: C. TAC

rev recurses to the end of the string first, then putchar(*s) runs while unwinding, so characters print in reverse order: last character 'T' first, then 'A', then 'C', giving TAC.

More C Programming questions

Exams that ask this topic