Home > Back-end >  The static effect
The static effect

Time:06-03

#include
Int fun (int I)
{
Return the I * I;
}
Int main ()
{
int i=0; I=fun (I);
For (; I & lt; 3; I++)
{
Static int I=1;
I +=fun (I);
Printf (" % d \ n ", I);
}
Printf (" % d \ n ", I);
return 0;
}
Results of 2 6 and 3, bosses tell me something about the detailed operation process, I value changes

CodePudding user response:

You have static understanding become global variables to understand, just this variable inside the loop only effective
Static int I=1; Only when the first time into the loop body is 1, belong to define when the initialization
Then came in again, or the last numerical

CodePudding user response:

This is who the burning question of the brain ah
Int I=0; I=fun (I);//at this point I=0

For (; i<3; I + +) {
Static int I=1; Redefined the I here, and it is the static type, every time come in to keep the last time the results
I +=fun (I);//at this point I use is that the static I; Is not the for statement I
The printf (I); This is it//2, 6, 42 output
}
Printf (I)//output 3, the body of this cycle is for the I, I, out of the loop body is invalid

This is the c language, if it is a c + +, in the loop with: : I still can continue to refer to the global, I do not have this definition
c

CodePudding user response:

In short, the loop body all the occurrences of the I, as another variable j to want to clear,

Because the static int I=1; This definition, has nothing to do with the static, is the definition of int I, let this I and for loop I produced the difference between

CodePudding user response:

For (; I & lt; 3; I++)
{
Static int I=1;
I +=fun (I);
Printf (" % d \ n ", I);
}
Equal to
Int j=1;
For (; I & lt; 3; I++)
{
J +=fun (j);
Printf (" % d \ n ", j);
}

CodePudding user response:

I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html
  • Related