Sorry if I did this wrong, i'm really new with C and haven't used stack overflow before. I'm trying to trace this simple recursive function by hand but am getting a different answer from the compiled code.
My thought process was
print 2 | n = 2-1 = 1 | 1 >= 0 | countdown(1)
print 1 | n = 1-1 = 0 | 0 >= 0| countdown(0)
print 0 | n = 0-1 = -1 | -1 is not >= 0|
print -1| END
void countdown(int n)
{
printf ("n = %d\t", n);
n--;
if (n >= 0)
{
countdown(n);
}
printf ("n = %d\t", n);
}
int main ( )
{
countdown(2);
return 0;
}
I expected to get: n = 2 n = 1 n = 0 n = -1
but the compiled code gives me: n = 2 n = 1 n = 0 n = -1 n = 0 n = 1
I'm not quite sure where the additional 0 and 1 come from after -1
printfin the function.