Examveda
Examveda

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

A. 5 4 3 2 1

B. 0 0 0 0

C. Infinite Loop

D. None of These

Answer: Option B

Solution(By Examveda Team)

The variable "i" is declared as static, hence memory for I will be allocated for only once, as it encounters the statement. The function main() will be called recursively unless I becomes equal to 0, and since main() is recursively called, so the value of static I ie., 0 will be printed every time the control is returned.

This Question Belongs to C Program >> C Miscellaneous

Join The Discussion

Comments ( 5 )

  1. Expired Director
    Expired Director :
    3 years ago

    I think it will be 4 3 2 1

  2. Madhuri Anagha
    Madhuri Anagha :
    5 years ago

    But printf statement is inside if loop, so when the value of variable i is 0, control comes out of the loop and there end the program. So none of these is the correct answer option D .

  3. Rahul Khatwani
    Rahul Khatwani :
    6 years ago

    Sir. In this program the print function is never called. As every till the i becomes 0 it returns to main() with executing the print function. And when the i becomes 0 the loop condition fails and it breaks.

  4. Suraj Yadav
    Suraj Yadav :
    7 years ago

    output will be:4 3 2 1

  5. Sourav Nanda
    Sourav Nanda :
    8 years ago

    plz explain
    souravkumarnanda@gmail.com

Related Questions on C Miscellaneous