Q:

Attempting to print array elements recursively

#include <stdio.h>

int printArray(a[], unsigned int n){
    if (n == 4){
        printf("%d", a[4]);
    }
    else {
        printf("%d", printArray(a,n+1)); // here is the error, I know. But why?
    }
}

int main(){

    int a[5] = {1,2,3,4,5};
    printArray(a,0);

}

I am a begginer with C. I am trying to print each of the elements of the array, but it only prints the last one correctly, and not the former ones. I know there's something about recursive way wrong. It's the first time I use recursive way without return and using printf. Maybe is this that misleaded me?
Output:
$ ./a.out 
5
0
1
1
0

A:

You should print the array item, not the return value of the function, Use this code
int printArray(int *a, unsigned int n){
    if (n == 4){
        printf("%d", a[4]);
    }
    else {
        printf("%d", a[n]);
        printArray(a,n+1);
    }
}   

