Hey, so I'm obviously new here, working my way through this book. I basically understand recursion, but I don't understand part of it. Can someone explain to me in plain terms what happens in the following code such that it works?
Code:
void pb (int n)
{
if (n != 0) {
pb (n / 2);
putchar('0' + n % 2);
}
}
So please help me understand what's happening here. Seeing as it does in fact work (printing n's binary form), it is easy to see how that would happen. But, the way I (incorrectly) see it, it just runs like this: Let's say main calls pb(55). I see pb recursively calling itself until n finally goes from 1 to 0, never reaching the putchar statement. At which point pb(0) is called--n == 0 so the if statement would not be entered. And that would be the end. Obviously that is not in fact what happens. Clearly I have some fundamental misunderstanding. Basically, why and how the putchar statement is executed. Thanks in advance for your help!