How to determine the output for the following code snippet?

#include <stdio.h>

void abc(){
    auto int a;
    static int s=5;
    a=++s;
    printf("%d %d\n",a,s);
    if(a<=7)
    abc();
    printf("%d %d\n",a,s);
}
int main(){
    abc();
    abc();
    return 0;   
}

4Comments
Prashant Sharma @prashantsharma
11 Jun 2017 10:02 pm

Storage Class

A storage class define scope as well as lifetime of a variable.

Storage classes in C are:

  1. Automactic
  2. Static
  3. External
  4. Register

Here, only auto and static are used so lets focus on that:

(I)

What do we mean by writing auto int a; ?

That the variable will be destroyed when the function exits (i.e) completes execution.

By "destroyed" it mean that the memory space allocated to the variable will be freed, hence any value stored in that variable will be lost.

(II)

What do we mean when we write static int s ; ?

That the variable will not be destroyed till the end of program (NOTE: its till the end of program ). Therefore, the value that is stored in such variable persists (i.e stored in the memory) and when the function is again called the value is preserved.So, any expression using such statis variable will access the preserved value.

Now,

Lets look at the program. (Keep in mind that variable a is auto and variable s is static)

Phase-1: when 1st abc() is called

  • abc() is called inside main()
  • s=5
  • after , a= ++s, a=6, s=6

output:

6 6

  • since, a <= 7 is True , abc() is again called
  • Now, s = 6,
  • due to a=++s, a=7 , s= 7

output:

6 6

7 7

  • again a <= 7 is True
  • after this iteration, a=8, s=8

output:

6 6

7 7

8 8

  • now, a <=7 is False
  • threfore the recursion stops

output:

6 6

7 7

8 8

8 8

7 8

6 8

Phase-2: when 2nd abc() is called.

  • abc() is called inside main()
  • s=8 (remember that it is static)
  • a = 9, s = 9 (due to a=++s)

output:

6 6

7 7

8 8

8 8

7 8

6 8

9 9

  • now, a <=7 is False
  • therfore, printf() is executed once more

output:

6 6
7 7
8 8
8 8
7 8
6 8
9 9
9 9

Hope this helps!

Keep in mind the concept of storage classes and recursion and you can understand this. If you find anything unclear about my explanation. Do not hesitate to ask! Keep Learning!

Divya Kathuria @divyakathuria05
11 Jun 2017 10:19 pm

After this iteration,

6 6

7 7

8 8

  • now, a <=7 is False
  • threfore the recursion stops

So on executing printf statement why the output is not only

6 6
7 7
8 8
8 8

and how 7 8 and 6 8 form a part of the output.

Prashant Sharma @prashantsharma
11 Jun 2017 10:42 pm

@divyakaturia05,

because, when the recursion stops, the control is retuned back to the parent functions :

See,

phase-1:

 abc() -->  abc() --> abc () [here recursion stops]

so now when the last abc() prints "8 8", it returns control back to 2nd abc() which was called during recusion. Now, this abc() will execute the last printf() hence, prints "7 8" ( remember s is static) and returns the control back to 1st abc() and lasttly the 1st abc()  executes last printf() and prints "6 8 "

hence, the output:

6 6

7 7

8 8

8 8

7 8

6 8

Hope this clears your doubt!

If still unsure, I will suggest to revise how recursion calls works then look at this again you will find it easy.If you get any doubt in recursion calls also you can ask here!

This might help:

https://www.hackerearth.com/practice/notes/working-of-recursive-function/

Divya Kathuria @divyakathuria05
11 Jun 2017 10:58 pm

Thank you for the solution!

My doubt is clear now

Pages