2

The following code gives output as 0 0 0 0 using Codeblocks.

int main()
{
 static int i=5;
 if(--i){
    main();
    printf("%d ",i);
 }   
}

I perfectly understands how the above code executes. However, when I removed 'static' from the code and used int i = 5, Ideone.com(online compiler) gave me runtime error and Codeblocks(using GCC) gave me nothing- even the terminal does not pop up.

I also tried placing the declaration part outside main i.e., static int i; and in main, I then gave i = 5;. Still, I am getting the above errors. I have no idea what is happening. Any help would be greatly appreciated.

PS: The program was found on a website and no explanation was given there.

dpaks
  • 375
  • 1
  • 13
  • 2
    You may want to read up what a static local variable is. For example: http://stackoverflow.com/q/572547/1025391 – moooeeeep Aug 27 '14 at 16:41
  • 1
    IIRC the `main` function has a very special status in C99 or C11 standard specification and is not allowed to be called (recursively). So it is undefined behavior. – Basile Starynkevitch Aug 27 '14 at 17:05

5 Answers5

4

If you remove the static that each call to main gets its own copy of i, initialized to 5, and so your recursion never terminates.

dohashi
  • 1,771
  • 8
  • 12
  • Thats understandable but what is the problem if I declare 'static int i' outside main()? – dpaks Aug 27 '14 at 16:41
  • 2
    @code_dweller Where did you assign i = 5? At the declaration or in main? If it is in main, then it will be re-assigned 5 each time main is called. – dohashi Aug 27 '14 at 16:49
1

When you declare static int i outside main() and initializing it inside main() to value 5 has a problem when you execute. For the first time if(4) makes it call your inner main() which executes outer main() making i set back to 5 leading it to infinite loop, hence no output you see because your if never fails and only one possibility it has is if(4).

int main()  // 1st main call
{
  static int i=5;
   if(--i){  // if(4), if(3), if(2), if(1)-> all four if's are true
             // if(0) fails 
   main();   // 2nd main, 3rd main, 4th main, 5th main -> corresponding to
             // above successful if's.   
             // When if(0), recursion ends, return 
   printf("%d ",i); // Now i is `0` and prints 4 zero's 
  }   
}
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
0

If you remove static, you get an infinite recursion before you output anything. The recursion leads to a stack overflow, and the program crashes.

The static means you operate always on the same variable, so you can count from 5 -> 0. if you remove it, you initialize it at every call with 5, and if(4) is always true. With static it is only initialized once.

Juri Robl
  • 5,614
  • 2
  • 29
  • 46
  • Why am I not getting output when I put declaration 'static int i' outside main()? – dpaks Aug 27 '14 at 16:45
  • 1
    Because you are `i = 5;` gets executed everytime. Only if you use it as initialization it doesn't get repeated everytime. – Juri Robl Aug 27 '14 at 16:49
0

when using static printf will show 4,3,2,1 and the recursion will end.

when not using static, theoretically (if the program would run) the printf will show 4,4,4,4.. and the recursion will never end.

shoham
  • 293
  • 1
  • 9
0

There is no problem in your code. CASE 1: if you write, it will give you output 0000 cause very time the you are decrementing the value of i. once it become 0. if condition will be false and it will print 0 four times.

int main()
    {
     static int i=5;
     if(--i){
        main();
        printf("%d ",i);
     }   
    }

CASE 2:

but if you remove the static, the in every recursive call you are creating a new variable i which will take 2 byte each time and consume memory(in stack). Once the stack full, it prompt runtime error and program will be crashed. The online compiler of http://ideone.com handle the runtime error in backend so that your program will not crashed.that why you are getting runtime error. and expected answer.

CASE 3:

and when you declare your variable outside like

static int i;
    int main()
        {
       i=5;
         if(--i){
            main();
            printf("%d ",i);
         }   
        }

In this case it will again cross the memory limit. here each time you are assigning the 5 to i and main() will take space in stack, again program crash.

Kailash Karki
  • 2,106
  • 1
  • 12
  • 6