I really don't get how we got these outputs

can somebody explain to me how did we get this output?

OUTPUT:

Q
B C D

This is my code:

#include <stdio.h>
char x = 'A';
void fun_1(int n) {
    if(n>0) fun_1(n-3);
    if(n == 0 ) printf("Q\n");
    printf("%c ", ++x);
};

int main()
{
    fun_1(6);    
    return 0;
}

4 answers

  • answered 2018-04-14 14:36 Maxime Chéramy

    Guys can somebody explain to me how did we get this output?

    I'm able to reproduce:

    % vi aaaa.c
    [ code copied in the editor ]
    % gcc aaaa.c
    % ./a.out
    Q
    B C D
    

    I hope that helped!

    (that was a joke, if it's not clear to everyone...)

    You can try to add a printf in the function fun_1. You see that it's called recursively 3 times with the values 6, 3 and 0.

    The last call will print the Q then the B. Then the second call will print the C and the first one the D.

  • answered 2018-04-14 14:36 SMA

    Here's the calls that would be made:

    fun_1(n=6)//from main
        fun_1(n=3)//since 6 > 0 {
            fun_1(n=0){//now n is not greater than zero, so don't recurse
                execute if n == 0, yes it is, so output Q
                print ++x, x was A now after ++x, x would be 'B'
            }
            since n <> 0, don't output Q
            print ++x, x was B, so print C which is after pre-incrementing x
        }
        since n <> 0, don't output Q
        print ++x, x was C, so print D which is after pre-incrementing x
    }
    

  • answered 2018-04-14 14:36 Pablo

    You have a recursive function, recursive means that at some point it calls itself. The terminal case (when the function stops calling itself) is when n == 0.

    When dealing with recursive functions, you have to think this way:

    Recursion level 0 (the first call)

    n == 6 ==> if(n>0) fun_1(n-3); is executed
    
    • Recursion level 1

      n == 3 ==> if(n>0) fun_1(n-3); is executed
      
      • Recursion level 2

        n == 0 ==>
            if(n == 0 ) printf("Q\n");  is executed, output is "Q\n"
        
            printf("%c ", ++x);         is executed, x is now B, output is B
        

        this is terminal case, therefore

      (back to)

      Recursion level 1

      printf("%c ", ++x);  is executed, x is now C, output is C
      

    (back to)

    Recursion level 0

    printf("%c ", ++x);  is executed, x is now D, output is D
    

    Now the recursive calls have ended and you are back in main. As you can see from the analysis of the recursion levels, the generated output is

    Q
    B C D
    

  • answered 2018-04-14 14:36 Ozan

    void fun_1(int n) {
    if(n>0) fun_1(n-3);
    if(n == 0 ) printf("Q\n");
    printf("%c ", ++x);
    };
    

    with the first call of this method in the main function, you send the value of 6.

    if(n>0) fun_1(n-3) with this statement, because 6>0, recursive process is started and 6-3=3 is sent back to fun-1 function. But here, the most important thing to keep in mind that, the first function which is created in memory is not terminated. For each recursive step, there will be created new functions for new n values. So here;

    printf("%c ", ++x);

    this statement will be working with the same number of recursive loop. Because you used, pre-incrementation as "++x", the char value x = 'A' will first be incremented and then printed. So for every recursive loop step, all the functions which are created are terminated by a sequence of creation, and they printed the pre-incremented value as in output.