ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
#include <stdio.h>
void f(int n) {
    if (n == 0) return;
    for (int i = 0; i < n; i++) printf("*");
    f(n - 1);
    printf("|");
}
int main(void) {
    f(3);
}
  1. A. ***|**|*|
  2. B. *|*|*|
  3. C. ******|||
  4. D. ***|||

Correct answer: C. ******|||

Descending, f(3) prints 3 stars, f(2) prints 2 stars, f(1) prints 1 star (6 stars total) before the base case f(0) returns with no output. Unwinding then prints '|' three times, once per returning call: 6 stars followed by 3 bars.

More C Programming questions

Exams that ask this topic