How does this program execute? [duplicate]

2019-09-22 09:51发布

问题:

This question already has an answer here:

  • unexpected output in C (recursion) 3 answers
#include <stdlib.h> 
#include <stdio.h> 
int main() 
{ 
    static int i = 5; 
    if(--i){ 
        main(); 
        printf("%d ",i); 
    }
    return 0;
}

The output of above code is 0 0 0 0. I get why it is 0 in the first place, but why is 0 printed three more times?

回答1:

Recall that the static variable is shared between all calls to main. Then consider the code as it is executed recursively:

int main() {
    static int i = 5; // 5
    if (--i) { // 4
        main() {
            if (--i) { // 3
                main() {
                    if (--i) { // 2
                        main() {
                            if (--i) { // 1
                                 main() {
                                     if (--i) // 0 (false)
                                     return 0;
                                 }
                                 printf("%d ",i); // 0
                            }
                            return 0;
                        }
                        printf("%d ",i); // 0
                    }
                    return 0;
                }
                printf("%d ",i); // 0
            }
            return 0;
        }
        printf("%d ",i); // 0
    }
    return 0;
}


回答2:

The crux is that since i is static, everything sets the same i variable. And main calls itself recursively until i is a falsy-value.

int main() 
{ 
  static int i = 5; 
  if(--i){ // i is now 4
    main(); 
      { 
        if(--i){ // i is now 3
          main(); 
            { 
              if(--i){ // i is now 2
                main(); 
                  { 
                    if(--i){ // i is now 1
                     main(); 
                        { 
                          if(--i){ // i is now 0, which is false, so main isn't called
                          }
                        }
                      printf("%d ",i); //print "0"
                   }
                 }
                printf("%d ",i);  //print "0"
             }
           }
          printf("%d ",i);  //print "0"
        }
      }
    printf("%d ",i);  //print "0"
  }
  return 0;
}


回答3:

Variable i is static. So the variable get value at the first time the program reaches line 5 and line 5 never execute anymore. and since variable i is static, every time that main got executed, it accesses the same i that it had accessed before. and if any function call changes the value of i, its values in other function calls will change. so while the value of i is not 0 the main function got called and when its value becomes equal to 0, main function returns and printf()s start to executing and printing i, which is 0



回答4:

as i is declared static, it will be on data segment(if you remove static it will be on stack and will have different values in each stack frame but here i=5 initialized at start everytime if you remove static your program will get into infinite function call loop, as i will never become zero so recursion will not terminate)

and as i is static whatever the change you make will be retained throught the function calls. when i reaches to zero recurssion ends as if(0) becomes false, when it ruturns, when it return it will print final value of i i.e zero



回答5:

It goes like this

static int i = 5
if (--i) //i == 4{
  if (--i) //i == 3{
    if(--i) // i ==2{ 
      if (--i) // i==1{
        if (--i) //i==0 this means i==false{
        }
        print (i) //i ==0
      }
      print(i) //i ==0
    }
    print(i) //i==0
  }
  print(i) //i==0
return 0

This is what happens when you execute that code, 4 recursive calls, once a call ends it print the variable i which is static so it's literally the same one for all the calls.

Why 4 prints and not 5?

Because the -- used as prefix works like this

i = i-1
if (i)

while the -- used as postfix works like this

if (i)
i = i-1