Can anyone explain why the printf executes or is that guy wrong?
Because main
returns.
This answer writes out the logic, but I see that you are still asking this in comments. So I'm going to say the same thing in a different way and hope that it is clearer to you.
Because the variable is static
, prior to the first call, the variable is declared and set to 4. Because it is static
, the same variable will be used every time. It just skips over the declaration and initialization if called again (or likely even the first time, as static
variables can be allocated before running).
In the first call, the variable is decremented to 3. 3 is a truthy value, so we do the block of the if
. This calls main
recursively.
In this second call, the variable is decremented to 2. 2 is still truthy in C. We call main
recursively again.
In this third call, the variable is decremented to 1. 1 is still truthy. We recurse to main
again.
In this fourth call, the variable is decremented to 0. 0 is falsey in C. We do not enter the block of the if
. We do not recurse. We do not print. There's nothing left to do in this call to main
. It returns at the end.
Now we're back in the third call. We just returned from the call to main
. We are still inside the if
block. The next statement is a printf
. The value of the variable is now 0. So we print a 0. Nothing left to do, we return at the end.
Now we're back in the second call. Still 0, so we print another 0 and return.
Now we're back in the first call. Still 0, so we print another 0 and return.
We printed 0 three times, so 000
As you note, on the way down, we don't print. We print on the way back up. By that time, the variable is 0. And we enter the then block of the if
three times. So we recurse and then print three times.
When you call a function recursively, you don't eliminate the previous call. You put it on the stack. When done, you come back to the place where you left and continue with the next statement.
You may want to think about how the following three pieces of code will react differently:
void recurse() {
static int i = 4;
if (--i) {
recurse();
printf("%d", i);
}
}
Prints 000 when called the first time. Does nothing thereafter.
void recurse(int i) {
if (--i) {
recurse(i);
printf("%d", i);
}
}
Will print 123 if called as recurse(4)
. As a parameter, we are dealing with a different i
each time the function is called.
void recurse(int i) {
if (--i) {
printf("%d", i);
recurse(i);
}
}
Prints 321 if called as recurse(4)
. Printing on the way down rather than when coming back up.
void recurse(int i) {
printf("%d", i);
if (--i) {
recurse(i);
}
}
Prints 4321 if called as recurse(4)
. It prints on the way down, rather than the way up.
void recurse() {
static int i = 4;
printf("%d", i);
if (--i) {
recurse();
}
}
Will print 4321. We're printing on the way down, not the way up. Note how the parameter and the static
variable give the same result this way. However, if this is called a second time, it will do nothing. The parameter would print the same thing again.
void recurse() {
int i = 4;
if (--i) {
recurse();
printf("%d", i);
}
}
Loops forever. Without the static
we make a new i
each time. This will run until it overloads the stack and crashes. No output because it never makes it to the printf
.
The easiest way to check this is to go somewhere like ideone.com and run the code. Asking why it prints something is reasonable for this site. But asking what it prints is something that you should answer yourself.