Where local variables and global variables are stored in memory in 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"

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. The scope of local variables also describes 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"

int main (void)


int s,a;


int b;


/*b Scope *


/*s,a Scope *


"Example 1.3"

int main (void)


int i=2,j=3,k;



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"

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.

int s1,s2,s3;

int vs (int a,int b,int c)


int V;





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.

int a=3,b=5; /*a,b for external variables * *

Max (int a,int b)/*a,b to external variable * *


int C;


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.2 Auto 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"

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.

f (int a)


Auto b=0;

Static c=3;



return (A+B+C);


int main (void)


int a=2,i;

for (i=0;i<3;i++)

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.