-3

why is the output contains 4 time 0's.The main call again and again until if condition become false and then it should be exit from if block.

#include <stdio.h>
    int main()
    {
        static int i=5;
        if(--i)
        {           
            main();
            printf("%d ",i);
        }
    }
suman
  • 11
  • 5

1 Answers1

0

Note the following.

  1. the int i is static.

  2. you are calling main reursively.

  3. In the if condition you have a pre decrement of i

Every time you call main, the value of i will be the same as the previous call. So i will decrement each time. Since this is --i it will be 4 the first time and go to 0.

After the innermost main function returns(i==0), the printf of the main before that will be executed.

But i is static and has a value of 0. So you get 4 zeros printed for each of the main functions.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31