C Language Learning Tutorial fifth chapter-Functions (8)

Source: Internet
Author: User

Variables can be stored in the form of "static storage" and "Dynamic storage".

A static storage variable is usually a storage unit that is set when the variable is defined and remains unchanged until the entire program ends. The global variables described in the 5.5.1 section belong to this type of storage. A dynamic storage variable is used to allocate a storage unit during program execution and is released immediately after use. A typical example is the form parameter of a function, which does not allocate a storage unit to parameters when the function is defined, but only when the function is called, and the function is released immediately after it is called. If a function is called multiple times, the storage unit of the parameter variable is repeatedly allocated and disposed. From the above analysis, we can know that the static storage variables are always present, while the dynamic storage variables sometimes disappear. We also refer to the existence of variables due to their different storage patterns. The lifetime represents the time that a variable exists. Lifetime and scope describe the characteristics of variables from two different angles of time and space, which are both connected and different. The type of storage that a variable belongs to should not be judged only from its scope, but should also have an explicit description of the storage types.

In the C language, the storage type for a variable is described in the following four ways:
Auto variable
Register Register variable
extern external variables
Static statically variable
Automatic variable and register variables belong to dynamic storage, and external and static variables belong to static storage mode. After describing the storage type of a variable, you know that the description of a variable should not only describe its data type, but also its storage type. Therefore the full form of the variable description should be: Storage type descriptor data type descriptor variable name, variable name ... e.g.
static int a,b; Description A,b is a static type variable
Auto Char c1,c2; Description C1,C2 is an automatic character variable
static int a[5]={1,2,3,4,5}; Description A is an array of static integers
extern int x,y; Description X,y is an external integer variable
The following four types of storage are described below:

The type descriptor for the automatic variable is auto.
This type of storage is the most widely used type in the C language program. The C language stipulates that variables that are not stored type descriptions in the function are considered automatic variables, that is, automatic variables can be omitted from the descriptor auto. Variables defined in the programs in the previous chapters are automatic variables where the type descriptor is not stored. For example:
{int i,j,k;
char c;
......
Equivalent to: {auto int i,j,k;
Auto Char c;
......
}
Automatic variables have the following characteristics:
1. The scope of an automatic variable is limited to the individual defining the variable. An automatic variable defined in a function that is valid only within that function. An automatic variable defined in a compound statement is valid only in that compound statement. For example:
int kv (int a)
{
auto int x,y;
{Auto char c;
Scope of/*C/
......
Scope of/*a,x,y/

2. An automatic variable is a dynamic storage method that allocates a storage unit to start its lifetime only when it is used, that is, when the function that defines the variable is called. The function call ends, freeing the storage unit, ending the lifetime. Therefore, the value of an automatic variable cannot be persisted after the function call has ended. An automatic variable that is defined in a compound statement cannot be reused after exiting a compound statement, or it will cause an error. For example, the following programs:
Main ()
{Auto int a,s,p;
printf ("\ninput a number:\n");
scanf ("%d", &a);
if (a>0) {
S=a+a;
P=a*a;
}
printf ("s=%d p=%d\n", s,p);
}

S,p is an automatic variable defined within a compound statement and can only be valid within that compound statement. The 9th line of the program is to exit the compound statement with the printf statement output s,p value, which will obviously cause errors.

3. Because the scope and lifetime of an automatic variable are limited within the individual that defines it (within a function or compound statement), a variable with the same name is allowed in different individuals without confusion. Even an automatic variable defined within a function can have the same name as an automatic variable defined in a compound statement within the function. Example 5.14 illustrates this situation.
[Example 5.14]
Main ()
{
auto int a,s=100,p=100;
printf ("\ninput a number:\n");
scanf ("%d", &a);
if (a>0)
{
auto int s,p;
S=a+a;
P=a*a;
printf ("s=%d p=%d\n", s,p);
}
printf ("s=%d p=%d\n", s,p);
}
This program defines the variable s,p as an automatic variable two times in the main function and in the compound statement. According to the C language, in the compound statement, the compound statement should be defined by the S,P function, so the value of s should be A + a,p value of a*a. The s,p after exiting the compound statement should be the s,p defined by main, and the value is given at 100 when initialized. From the output results can be analyzed two s and two p although the variable name is the same, but it is two different variables.

4. No initialization assignment can be made for an automatic variable of a constructed type, such as an array.

Second, the type descriptor for external variables external variables is extern.

External variables are described in the previous introduction of global variables. Here are some additional features of external variables:
1. External variables and global variables are references to two different angles of the same class of variables. Global change is proposed from its scope, and the external variable is presented from its storage mode, indicating its lifetime.

2. When a source program consists of several source files, external variables defined in one source file are also valid in other source files. For example, there is a source program composed of source files f1.c and f2.c: F1. C
int a,b; /* External variable definition * *
char c; /* External variable definition * *
Main ()
{
......
}
F2. C
extern int a,b; /* External Variable description * *
extern char C; /* External Variable description * *
func (int x,y)
{
......
}
A,b,c Three variables are used in both f1.c and f2.c two files. A,b,c are defined as external variables in the f1.c file. Use extern to describe three variables as external variables in the f2.c file, indicating that they have been defined in other files, and that the type and variable names of these variables are no longer allocated memory space by the compiled system. External variables of the constructed type, such as arrays, can be initialized at the time of the description, and if no initial value is assigned, the initial value of the system is automatically defined as 0.

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.