I was playing around with recursion and did this simple function. I was assuming that it would print out 9-0 to stdout, but, it prints 0-9. I can’t see how that happens at all.
int main()
{
rec(10);
return 0;
}
int rec(int n){
if(n > 0)
printf("%d\n", rec(n -1));
return n;
}
As Michael Burr says in the comments, if you want to see what’s going on, compile with debugging symbols enabled, like this:
Then run with gdb like so.
Then, to start things going, type
Which breaks at the first call in the main function. Type
to get to the next line in the code, and from then on, just press enter to keep repeating the last command. If you’re happy, type
continueto stop stepping through. You’ll see the values and evaluated lines at each stage which’ll confirm the above answers.Hope that provides some useful info.