Home > Blockchain >  I wanted to know the details on how it's working for the following code
I wanted to know the details on how it's working for the following code

Time:03-30

I got the output 00246 but i don't undstand the code, can some one elaborate it please?

int print(int nb)
{
    if (nb < 0) 
    {
        return (0);
    }
    printf("%d", nb   print(nb - 1));
    nb --;
    return (nb);
}

int main(void)
{
    print(4);
    return (0);
}

This is the code.

CodePudding user response:

I'd recommend that you write down on a pad of paper the different values of nb that occur.

Be aware that the print function in this line: printf("%d", nb print(nb - 1)) will not be called until the print(nb-1) call returns a value. Ultimately this means that the first value printed will be from the last level of recursion.

A change to your code can show you how this works:

    int print(int nb, int level)
    {
        printf("\nEnter level %d nb= %d", level, nb);
        if (nb < 0) 
        {
            printf("\nExit level %d nb =%d return 0", level, nb);
            return (0);
        }
        printf("\nOriginal Output  level %d nb=%d",level, nb   print(nb - 1, level 1));
        nb --;
        printf("\nExit level %d nb=%d", level, nb);
        return (nb);
    }
    
    int main(void)
    {
        int level=0;
        print(4, level);
        return (0);
    }

this gives an output of:

Enter level 0 nb= 4
Enter level 1 nb= 3
Enter level 2 nb= 2
Enter level 3 nb= 1
Enter level 4 nb= 0
Enter level 5 nb= -1
Exit level 5 nb =-1 return 0
Original Output level 4 nb=0
Exit level 4 nb=-1
Original Output level 3 nb=0
Exit level 3 nb=0
Original Output level 2 nb=2
Exit level 2 nb=1
Original Output level 1 nb=4
Exit level 1 nb=2
Original Output level 0 nb=6
Exit level 0 nb=3

In the changed code I've added a variable level which tags a number to each level of recursion.

If you follow the output along with the code you can see that at the top level nb starts with a value of 4.

In the line printf("\nOriginal Output level %d nb=%d",level, nb print(nb - 1, level 1)); you call your print function, this takes you to your next level of recursion where on Entry nb is 3.

At this point the printf statement has not been called as the code requires a return value from the print function.

The code runs until we again call print whereby the code enters the next level of recursion.

At each time print is called all local variable values at that point and a pointer to where the function was called is placed on the stack so that the flow of code can return to where it was called once it has been completed.

This cycle continues until at level 5 where nb is less than 0 and so the code returns the value of 0, it does not reach the printf statement at that level and hence the value of 0 is returned to where the print function was called on level 4. This is done by using and then removing the data that was placed on the stack.

The printf call can now be run as we have a value returned from the print function call. At this point the local value of nb is 0, as indicated by the statement Enter level 4 nb= 0 in the output. This local value is added to the return value from the call to print i.e. 0, 0 0=0 and so you see:

Original Output level 4 nb=0

After the printf code completes for that level nb is decremented returning the value of -1.

Exit level 4 nb=-1

Again the stack is rolled back one level, now at level 3 the return value of -1 is added to the level 3 value of nb (Enter level 3 nb= 1), 1-1=0 so the output of the printf is:

Original Output level 3 nb=0

The cycle continues until all of the stack levels have been rolled back. The output shows all of the stages down to level 5 that is placed onto the stack and from there all of the stages as it is rolled back until we are back at level 0.

CodePudding user response:

The recursive calls don't stop till we get to the call where nb==-1. This returns 0.

The call before that can now resume. In that call, nb==0. We add that 0 to the 0 returned by the call in which nb==-1 to get 0, and print that. Then we return nb-1 (because we say nb--), which is -1.

The call before that can now resume. In that call, nb==1. We add that 1 to what we got from the nb==0 call, which was -1. So we print 1 -1, or 0. Then we subtract 1 from the current nb, which is 1, to get 0, and return that.

The call before that can now resume. In that call, nb==2. We add that 2 to what we got from the nb==1 call, which was 0, and print that resulting 2. Then we subtract 1 from the current nb, which is 2, to get 1, and return that.

The call before that can now resume. In that call, nb==3. We add that 3 to what we got from the nb==2 call, which was 1, and print the resulting 4. Then we subtract 1 from the current nb, which is 3, to get 2, and return that.

The call before that can now resume. In that call, nb==4. We add that 4 to what we got from the nb==3 call, which was 2, and print the resulting 6. Then we subtract 1 from the current nb, which is 4, to get 3, and return that to main, and we're done.

So that's the trace. You can do this as suggested, yourself, with a bunch of printf's, or in the debugger, as I did.

But the question remains: what is this for? Is it just a mystery program to solve? If not, it could probably be simplified to make it more comprehensible -- but for that we'd have to know what it's meant to do.

  • Related