The use of static in C language detailed example Analysis _c language

Source: Internet
Author: User
Tags reserved

Static in C can be used to modify variables, or to modify functions.
First look at the time to modify the variable. Variables in C can be divided into the existence of global data areas, stacks and heaps. In fact, we usually say the stack is stack rather than heap, do not confuse.

Copy Code code as follows:

int A;
int main ()
{
int b;
int c* = (int *) malloc (sizeof (int));
}

A is a global variable, B is a stack variable, and C is a heap variable.
Static the modification of a global variable can be considered to limit the only reference to this variable in this file. Some programs are composed of a lot of. c files. Variables can be referenced to each other, but after the static adornment is added, this variable can only be referenced by functions in this file.

Static on a stack variable, you can think of the life cycle of the stack variable extended to the end of program execution. In general, the life cycle of the stack variable is managed by the OS, and the life of the stack variable is over when the stack is retired. But after adding the static modifier, the variable is no longer stored on the stack, but is stored with the global variable. At the same time, it cannot be used after you leave the function that defines it, but if you call the function that defines it again, it can continue to work and save the value left after the previous call.

A static modification of a function is similar to that of a global variable, and can only be called by a function in this file, not by a function in the other file of the same program.

File A.C

Copy Code code as follows:

static int i; Only in a file
Int J; Used in engineering.
static void Init ()//only in a file
{
}
void CallMe ()//in engineering
{
static int sum;
}

The above global I variables and init () functions can only be used in A.C files, and the global variable sum is scoped only to the CallMe. The global limits of the variable J and function CallMe () are extended to the entire project file. So you can invoke the extern keyword in the following B.C. extern tells the compiler that the variable or function is already defined in other files.

File B.C

Copy Code code as follows:

extern Int J; Call a file in a
extern void CallMe (); Call a file in a
int main ()
{
...
}

An additional use of extern is when C and C + + are mixed programming if C + + calls a function or variable defined by C source file, then add extern to tell the compiler to name the function in C mode:

File A.cpp call A.C inside the variables I and function CallMe ()

Copy Code code as follows:

extern "C"///in C + + file call variable
{
Int J;
void CallMe ();
}
int main ()
{
CallMe ();
}

two static law:
A, if the global variable only in a single C file access, you can modify this variable to static global variables, to reduce the coupling between modules;
B, if the global variable is only accessed by a single function, this variable can be changed to the static local variable of the function to reduce the coupling between the modules;
C, the design and use of access to dynamic global variables, static global variables, static local variables of the functions, you need to consider the problem of reentrant;

Global variables have both external and static storage methods.
(1) Global variables are generally stored in external storage mode and are defined with reserved word extern. At this point, the scope of the variable is all the program files that make up the entire program, that is, the external variables that are defined to be used by other program files.
Use such a global variable must be very cautious, once the error, will affect the entire program.

(2) If you want global variables to be limited to the use of this program file, and other program files can not be referenced, then you must define its storage mode as static storage, with reserved word static to define. This is called a static external variable.
For example, in the previous example file Filel. C, if you make this definition:
static int A:
The scope of variable A is narrowed down to the filel of this program file. C, File file2. cannot be referenced in C.
It is noteworthy that the global variable plus static, defined as static storage, does not mean that the static storage, but not static, is dynamic storage. Both forms of global variables (external variables) are statically stored, all at compile time allocating storage space, but scopes are different. The use of static external variables facilitates isolation errors and facilitates modular programming.

(3) The default storage mode for global variables is external storage.
The program in the previous section does not see the storage category definition of the variable, and actually takes the default storage of the variable. The local variable is auto, and the global variable is in extern mode. This is also so far, we have not seen in the program of Auto, extern, etc. reasons.
At this point, we have a comprehensive discussion of the storage categories and data types of variables, and here is a summary.

1. General form of variable definition
Storage category data type variable table;

2. The role of a variable definition
① specify the range of variables to be evaluated.
The ② prescribes the operation of the variable.
③ specify the scope of the variable.
④ Specify how the variables are stored.
⑤ Specify the storage space that the variable occupies.

3. Local variables and global variables
Divides the variables into local and global variables from the scope angle. The storage categories they take are as follows:

Local variables:
① an automatic variable, that is, a dynamic local variable (leaving the function, the value disappears).
② a static local variable (leaving the function, the value is still retained).
③ register variable (away from function, value disappears).
The ④ form parameter can be defined as an automatic variable or a register variable.

Global variables:
① static external variables (used only in this program file).
② external variables (that is, non-static external variables that allow other program file references).

4. Dynamic storage and static storage
Variable storage can be divided into dynamic storage and static storage from the time the variable exists. Static storage is present while the entire program is running, while dynamic storage allocates the storage cells temporarily when the function is called.

Dynamic storage:
① an automatic variable (valid within a function).
② Register variable (valid within function).
③ form parameters.

Static storage:
① static local variables (valid within the function).
② static external variables (valid in this program file).
③ external variables (the entire program can be referenced).

5. static storage and dynamic storage
Variable storage can be differentiated into static and dynamic storage from the location where the value is stored:
In-memory static storage area:
① static local variables.
② static external variables.
③ external variables (can be referenced by other files in the same program).
In-Memory dynamic storage: Automatic variables and formal parameters.
Registers in the CPU: register variables.

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.