unexpected output in C (recursion)

前端 未结 3 1603
迷失自我
迷失自我 2020-11-28 00:03
int main(void) {
 static int=5;
 if(--i) {
    main();
    printf(\"%d\",i);
   }
 }

the output of above program is---

0000

But I t

3条回答
  •  -上瘾入骨i
    2020-11-28 00:38

    1. You set a static variable i to 5
    2. You recurse on main until i becomes zero.
    3. The recursion unwinds with i being zero.
    4. This then calls the printf

    There lies the answer.

    You can prove this by using a debugger

提交回复
热议问题