ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
#include <stdio.h>
void inc(int a[], int n) {
    if (n == 0) return;
    a[n - 1]++;
    inc(a, n - 1);
}
int main(void) {
    int arr[3] = {10, 20, 30};
    inc(arr, 3);
    printf("%d %d %d", arr[0], arr[1], arr[2]);
}
  1. A. 10 20 30
  2. B. 10 20 31
  3. C. 11 20 30
  4. D. 11 21 31

Correct answer: D. 11 21 31

An array parameter decays to a pointer, so inc modifies the caller's array directly, not a copy. Each call increments a[n-1] before recursing (a[2], then a[1], then a[0]), leaving 11 21 31.

More C Programming questions

Exams that ask this topic