Home > Software engineering >  C language for help
C language for help

Time:09-19

 # include 
Int fun ()
{
Static int x=1;
X *=2;
Return the x;
}
Void main ()
{
Int I, s=1;
for(i=1; i<=3; I++)
S *=fun ();
Printf (" % d ", s);

}

Local variables, and should not be the result is 8, how is equal to 64?

CodePudding user response:

Static int x=1;//execute only once, not three times

CodePudding user response:

 
Int fun ()
{
Static int x=1;
X *=2;
Return the x;
}
Void main ()
{
Int I, s=1;
for(i=1; i<=3; I++)
{
S *=fun ();
Printf (" % d=% d \ n ", I, s);//1=2; 2=8; 3=64
}
}

CodePudding user response:

Fun function carried out three times, and x is a static variable, in the execution of a function effectively, becomes a global variable

CodePudding user response:

"Turned into a global variable is the function" fun () to see the static variables.

CodePudding user response:

You place the order with the debug step in to see not clear ~

CodePudding user response:

Static int x=1, this is a static variable is equivalent to a global variables is equivalent to the statement on the outside of the all the functions performed only once all initialized to 1

CodePudding user response:

Global variables, static, and local variables,,, to distinguish the building Lord!!!!!
  • Related