Home > Back-end >  About the difference between the global variable and a local variable in C language
About the difference between the global variable and a local variable in C language

Time:11-21

1. The average global variable
Concept: define variables in the function, called global variables;
scope: in the "" in the whole project, only need to be defined in a source file, and then under the project can be used in all the source files, using extern declaration,
life cycle: , according to the process began with the end of the process and end,
memory allocation:
static global areaEg:
 
# include
# include

Int a;//the common global variables defined

Int main ()
{
Printf (" a=% d \ n ", a);//the default value of output a

A=100;
Printf (" a=% d \ n ", a);//to give a assignment, the output value of a

system("pause");
Return (0);
}

Results:


Note: if there is no initialization common global variables, the default initialization of 0,

2. A static global variable
Concept: define variables in the function, and use the static keyword modified variable is called a static global variable;
scope: in a source file, are defined in a source file, and then in a source file can use, but cannot be used in other source files,
life cycle: , according to the process began with the end of the process and end,
memory allocation:
static global area
2. Ordinary local variable
Concept: define variables in the function, common local variables;
scope: starting from the definition of the position, to define its right curly brace, exist, only during the function performs function calls after the execution, a variable has been revoked, its memory also be retrieved,
life cycle: starting from the definition to the end of the function, the variable was cancelled after the function call, memory is recycling,
memory allocation:
stack area
2. The static local variable
Concept: define variables in the function, and use the static keyword modified variable is called a static local variable;
scope: local function, it is initialized only once, the first time since the initialized always there until the end of the program is running, and the difference between a local variable functions performed also exists, the
life cycle: , according to the process began with the end of the process and end,
memory allocation:
static global area
Eg:
 # include 
# include

Int a;//the common global variables defined
Static int b;//define a static global variable

Int fun1 ()
{
A=10;
+;
Return (a);
}

Int fun2 ()
{
B=10;
b++;
Return (b);
}

Void fun3 ()
{
Int c=10;
C + +;
Printf (" \ n c=% d ", c);//output c after the operation the value of the
}

Void fun4 ()
{
Static int d=10;
D++;
Printf (" \ n d=% d ", d);//output d after the operation the value of the
}
Int main ()
{
Printf (" a default=% d \ n ", a);//the default value of output a
Printf (" b default=% d \ n ", b);//the default value of the output b

Fun1 ();
Fun1 ();
Printf (" a=% d \ n ", a);//output value after a run of

Fun2 ();
Fun2 ();
Printf (" \ n b=% d ", b);//after the operation the value of the output b

Fun3 ();
Fun3 ();

Fun4 ();
Fun4 ();

system("pause");
Return (0);
}



You can see that the static local variables is just at the time of the first call to open up space for assignment, after the function, will not release the space; To call a function of time, later won't open up space for its, also won't initialise, or previous variables,


CodePudding user response:

To illustrate their ideas, inventions and coined the additional terms, this is combined with the identifier and the scope of the variable of survival and invention of new terms, talented, QinFu you so much!

CodePudding user response:

Static local variables is just at the time of the first call to open up space for assignment

There is a problem

CodePudding user response:

The original poster can the dynamic application space, such as malloc/free, new/delete

CodePudding user response:

The learning to learn
Next:c
  • Related