Just change array[size] in your function to array
Code [to print last to first] :
#include <stdio.h>
int print(int array[],int size) {
if(size>0) { // changed this too
printf(" %d",array[size-1]);
return print(array,size-1); // note this carefully.
}
}
int main() {
int arr[]={1,4,6,9,0,3};
print(arr, sizeof(arr)/sizeof(int)); // changed to correct size [sizeof is generic than just mentioning in the size]
return 0;
}
code [to print first to last]
#include <stdio.h>
int print(int array[],int size) {
if(size>0) { // changed this too
print(array,size-1); // note this carefully.
printf(" %d",array[size-1]);
}
}
int main() {
int arr[]={1,4,6,9,0,3};
print(arr, sizeof(arr)/sizeof(int)); // changed to correct size [sizeof is generic than just mentioning in the size]
return 0;
}
at OP's request, explanation of how it works first to last.
Mathematical Explanation :
Let print (arr,size) be the function that prints 0 to size array.
Now print(arr,size+1) would be
print(arr,size); printf(arr[size])
Now see the code again.
Intuitively, if you printing first to last, you have to print lower elements first and the highest element at last.
If you see the original code, you had printed nth element first and hence the reverse printing.