A deep discussion on the location of local variables and global variables in memory in C language _c language

Source: Internet
Author: User
storage categories for local and global variable variables in C language (Static,extern,auto,register)

1. Local and global variables
When discussing the shape parametric of a function, it was mentioned that the form parametric allocates memory cells only during the call, and the call ends immediately. This indicates that the shape parametric is valid only within the function, leaving the function no longer available. The scope of the validity of this variable is called the field of the variable. Not only for parameter variables, all the quantities in C language have their own scope. Variable descriptions vary in the way they are scoped. C language variables, by scope can be divided into two types, namely, local variables and global variables.
1.1 Local Variables
Local variables are also called internal variables. A local variable is defined within a function. Its scope is limited to the function, it is illegal to leave the function to use this variable.
"Example 1.1"
Copy Code code as follows:

int F1 (int a)/* function f1*/
{
int b,c;
}
A,b,c Effective
int f2 (int x)/* Function f2*/
{
int y,z;
}
X,y,z Effective
int main (void)
{
int m,n;
}
M,n Effective

Three variables are defined within the function F1, a is a formal parameter, and b,c is a general variable. In the range of F1 A,b,c is valid, or the scope of a,b,c variable is limited to F1. Similarly, the scope of X,y,z is limited to F2. The scope of the m,n is limited to the main function. About the scope of local variables Also describe the following points:
1 The variables defined in the main function can also be used only in the main function and cannot be used in other functions. Also, you cannot use variables defined in other functions in the main function. Because the main function is also a function, it is parallel to other functions. This is different from other languages and should be noted.
2 The parameter variable belongs to the local variable of the modulated function, and the argument variable belongs to the local variable of the keynote function.
3 allows the use of the same variable names in different functions, which represent different objects, assign different units, do not interfere with each other, and do not confuse. As in the previous example, the parameters and arguments have variable names of n, which are fully permitted.
4 A variable can also be defined in a compound statement, and its scope is within the compound statement scope only.
"Example 1.2"
Copy Code code as follows:

int main (void)
{
int s,a;
{
int b;
S=a+b;
/*b Scope *
}
/*s,a Scope *
}

"Example 1.3"
Copy Code code as follows:

int main (void)
{
int i=2,j=3,k;
K=i+j;
{
int k=8;
printf ("%d\n", K);
}
printf ("%d\n", K);
}

This program defines the I,J,K three variables in main, where K does not assign an initial value. In the compound statement, we define a variable k and assign the initial value to 8. It should be noted that these two k are not the same variable. Outside a compound statement, the k that is defined by main is active, while within a compound statement the k that is defined within the compound statement works. So the K in line 4th of the program is defined as main, and the value should be 5. Line 7th output k value, the line in the compound statement, by the compound statement within the definition of K function, its initial value is 8, so the output is 8, the 9th line output i,k value. I is valid throughout the program, the 7th row of I assignment is 3, so the output is also 3. and Line 9th is outside the compound statement, the output of k should be defined by main K, this K value from line 4th has been 5, so the output is also 5.

1.2 Global Variables
A global variable, also called an external variable, is a variable defined outside the function. It does not belong to a function, it belongs to a source program file. Its scope is the entire source program. Using global variables in a function is generally described as a global variable. Only global variables that are described within a function can be used. The descriptor for the global variable is extern. However, a global variable defined before a function can be used within that function to no longer be explained.
"Example 1.4"
Copy Code code as follows:

int a,b; /* External Variable * *
void F1 ()/* function f1*/
{
......
}
float X,y; /* External Variable * *
int FZ ()/* function fz*/
{
......
}
Int main (void)/* Main function * *
{
......
}

From the example above you can see that a, B, X, y are all external variables defined outside the function, and are global variables. However, x,y are defined after the function F1, and there is no description of x,y in the F1, so they are not valid within the F1. The A,B definition is at the top of the source program, so it can be used without explanation in F1,F2 and main.
"Example 1.5" to enter the l,w,h of the square body. The volume and three surface x*y,x*z,y*z area.
Copy Code code as follows:

int s1,s2,s3;
int vs (int a,int b,int c)
{
int V;
V=a*b*c;
S1=a*b;
S2=b*c;
S3=a*c;
return v;
}
int main (void)
{
int v,l,w,h;
printf ("\ninput length,width and height\n");
scanf ("%d%d%d", &l,&w,&h);
V=vs (L,W,H);
printf ("\nv=%d,s1=%d,s2=%d,s3=%d\n", V,S1,S2,S3);
}

The "example 1.6" external variable has the same name as a local variable.
Copy Code code as follows:

int a=3,b=5; /*a,b for external variables * *
Max (int a,int b)/*a,b to external variable * *
{
int C;
c=a>b?a:b;
return (c);
}
int main (void)
{
int a=8;
printf ("%d\n", Max (a,b));
}

If an external variable has the same name as a local variable in the same source file, the external variable is "masked" within the scope of the local variable, that is, it does not work.

2. Storage categories for Variables
2.1 Dynamic storage mode and static dynamic storage mode
As described earlier, from the scope of the variable (that is, from the space) point of view, can be divided into global variables and local variables.
From another angle, it can be divided into static storage mode and dynamic storage mode from the point of time (i.e. lifetime) of variable value.
Static storage: Refers to the way in which fixed storage space is allocated during program operation.
Dynamic storage: The way in which storage space is dynamically allocated as needed during program operation.
The user storage space can be divided into three parts:
1) the program area;
2) static storage area;
3 dynamic storage area;
The global variables are all stored in the static store, and the global variables are allocated to the store when the program starts executing, and the program is released when the line is finished. In the process of program execution, they occupy a fixed storage unit, but do not dynamically allocate and release;
The Dynamic store holds the following data:
1) function form parameter;
2) automatic variable (local variable without static declaration);
3 Function call real site protection and return address;
For these data, the dynamic storage space is allocated at the beginning of the function call and the space is freed when the function ends.
In the C language, each variable and function has two properties: the data type and the storage category of the data.

2.2auto Variable
Local variables in functions, such as those not specifically declared as static storage categories, are dynamically allocated storage space, and data is stored in dynamic stores. The formal parameters in a function and the variables defined in the function, including those defined in the compound statement, are such that the system allocates storage space to the function when it is called and automatically frees the storage space at the end of the function call. This type of local variable is called an automatic variable. Auto variables are declared with the keyword auto as the storage category.
"Example 1.7"
Copy Code code as follows:

int f (int a)/* defines F function, A is the parameter * *
{
auto int b,c=3; /* Define B,C automatic variable * *
}

A is a formal parameter, the B,C is an automatic variable, and C is assigned an initial value of 3. After the F function is executed, the storage unit of the A,B,C is automatically released.
Keyword auto can be omitted, auto does not write is implied as "Automatic storage category", belongs to the dynamic storage mode.

2.3 Declaring local variables with static
Sometimes you want the value of a local variable in a function not to disappear after the function call ends, and then you should specify the local variable to be "static local variable" and declare it with the keyword static.
"Example 1.8" examines the value of a static local variable.
Copy Code code as follows:

f (int a)
{
Auto b=0;
Static c=3;
b=b+1;
c=c+1;
return (A+B+C);
}
int main (void)
{
int a=2,i;
for (i=0;i<3;i++)
printf ("%d", f (a));
}

description of static local variables:
1 static local variables belong to the static storage category, the storage unit is allocated in the static storage area. is not released during the entire run of the program. But the automatic variable (that is, dynamic local variable) belongs to the dynamic storage category, which occupies the dynamic storage space, and the function call is released after the end.
2 static local variables at compile-time initial value, that is, only once the initial value, and the initial value of the automatic variable is in the function call, every time the function is called again to the initial value, equivalent to the execution of an assignment statement.
3 If the local variable is defined without an initial value, then for static local variables, the compile-time automatically assigns an initial value of 0 (for numeric variables) or null characters (for character variables). For an automatic variable, its value is an indeterminate value if it is not assigned an initial number.
"Example 1.9" prints the factorial value from 1 to 5.
Copy Code code as follows:

int FAC (int n)
{
static int f=1;
F=f*n;
return (f);
}
int main (void)
{
int i;
for (i=1;i<=5;i++)
printf ("%d!=%d\n", I,FAC (i));
}

2.4register Variable
In order to improve efficiency, C language allows local variables to be placed in the CPU registers, this variable is called "register variable", with the keyword register as a statement.
"Example 2.0" uses register variables.
Copy Code code as follows:

int FAC (int n)
{
register int i,f=1;
for (i=1;i<=n;i++)
F=f*i;
return (f);
}
int main (void)
{
int i;
for (i=0;i<=5;i++)
printf ("%d!=%d\n", I,FAC (i));
}

Description:
1 only local automatic variables and formal parameters can be used as register variables;
2 The number of registers in a computer system is limited and can not define any number of register variables;
3 Local static variables cannot be defined as register variables.

2.5 declaring external variables with extern
An external variable (that is, a global variable) is defined outside of the function, and its scope starts at the variable definition and ends at the end of this program file. If the external variable is not defined at the beginning of the file, its valid scope is limited to the end of the file at the definition. If the function before the definition point wants to refer to the external variable, you should use the keyword extern to declare the variable as an external variable before referencing it. Indicates that the variable is an external variable that has already been defined. With this declaration, you can legitimately use the external variable from the declaration.
"Example 2.1" declares an external variable with extern, extending the scope in the program file.
Copy Code code as follows:

int max (int x,int y)
{
int z;
z=x>y?x:y;
return (z);
}
int main (void)
{
extern a,b;
printf ("%d\n", Max (a,b));
}
int a=13,b=-8;

Description:The external variable a,b is defined in the last 1 lines of this program file, but the external variable a,b cannot be referenced in the main function because the position defined by the external variable is after the function main. Now we use extern for A and B in the main function to "external variable declarations", and we can legitimately work with the external variables A and b from the declaration.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.