The use of C-language static

Source: Internet
Author: User
Tags class definition

Static can be used to modify variables in C, or to modify functions.
First look at the time to modify the variables. Variables in C can be divided into existing global data regions, stacks and heaps. In fact, we usually say that stacks are stacks rather than heaps, do not confuse.
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 modification of global variables can be considered as limiting the only reference to this variable in this file. Some programs are composed of a lot of. c files. You can reference variables to each other, but after adding the static modifier, this variable can only be referenced by the function in this file.
Static modification of the stack variable can be considered as extending the life cycle of the stack variable to the end of the execution of the program. In general, the life cycle of a stack variable is managed by the OS, and the life of the stack variable ends in the process of unwinding the stack. But after adding the static modifier, the variable is no longer stored in the stack, but is stored with the global variable. At the same time, it cannot be used after the function that defines it, but it can continue to be used if the function that defines it is called again, and the value left after the previous call is saved.
Static modification of a function is similar to a modification of a global variable, and can only be called by a function in this file, not by a function in another file in the same program.

File A.C
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 global I variable and the init () function above can only be used in the A.C file, and the scope of the global variable sum is only in CallMe. The global limits of the variable J and function CallMe () are extended to the entire project file. So it can be called with the extern keyword in the B.C below. extern tells the compiler that this variable or function is already defined in other files.

File B.C
extern Int J; Called in the a file.
extern void CallMe (); Called in the a file.
int main ()

The other usage of extern is that when C and C + + are mixed programming if C + + calls a function or variable defined by the C source file, add extern to tell the compiler to name the function in C mode:

File A.cpp call A.C inside the variable i and function CallMe ()
extern "C"//in C + + file to invoke variable
Int J;
void CallMe ();
int main ()
CallMe ();

Two static laws:

A, if the global variable is only accessed in a single C file, you can modify the variable to a static global variable to reduce the coupling between modules;

B, if the global variable is accessed only by a single function, then the variable can be changed to the static local variable of the function, in order to reduce the coupling degree between the modules;

C, the design and use of access to dynamic global variables, static global variables, static local variables of the function, you need to consider the re-entry problem;

NOTE: Coupling (English: coupling,dependency, or coupling force or coupling) is a software metric that refers to the degree to which information or parameters between modules and modules depend on each other in a program. Cohesion is a concept that is relative to coupling, in general, low coupling represents cohesion, and vice versa. The coupling and cohesion are proposed by the Rayry Constantine of the concept of structural design. Low coupling is the characteristic of well-structured programs, and the readability and maintainability of low-coupling programs will be better.

There are two ways to store global variables, both external and static.
(1) Global variables are generally stored in an external storage mode, defined with the reserved word extern. At this point, the scope of the variable is all program files that make up the entire program, that is, the external variables that are defined for use 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 the global variable to be limited to the use of this program file, and other program files cannot be referenced, you must store it as a static storage method, defined by the reserved word static. This is called a static external variable.
For example, in the previous example file Filel. C, if you make such a definition:
static int A:
The scope of variable A is reduced to this program file Filel. C, File file2. cannot be referenced in C.
It is worth noting that adding static to the global variable, defined as static storage, does not mean static storage, and is dynamic storage, without static. Both forms of global variables (external variables) are stored statically, and are allocated at compile time, but with different scopes. The use of static external variables facilitates the isolation of errors and facilitates modular programming.
(3) The default storage method for global variables is external storage.
The program in the previous section did not see the variable's storage class definition, in fact the default method of storing the variable. The local variable is auto-mode, and the global variable is taken in extern mode. This is also so far, we have not seen in the program auto, extern and other reasons.
At this point, we have a comprehensive discussion on the storage categories and data types of variables, and make a summary here.
1. General form of variable definition
Storage class data type variable table;
2. Role of variable definition
① specifies the range of variables to be evaluated.
② Specifies the operation of the variable.
③ specifies the scope of the variable.
④ Specifies how variables are stored.
⑤ Specifies the storage space that the variable occupies.
3. Local variables and global variables
The variables are divided into local variables and global variables from the scope perspective. The storage categories they take are as follows:
Local variables:
① an automatic variable, i.e. a dynamic local variable (leaving the function, the value disappears).
② static local variables (leave function, values remain).
③ The register variable (leaving the function, the value disappears).
The ④ form parameter can be defined as an automatic variable or a register variable.
Global variables:
① static external variables (only used by this program file).
② external variables (that is, non-static external variables, allowing other program file references).
4. Dynamic storage and static storage
Variable storage can be divided into dynamic storage and static storage from the time of variable existence. Static storage exists while the entire program is running, while dynamic storage allocates the storage unit temporarily when the function is called.
Dynamic storage:
① automatic variable (valid within function).
② Register variable (valid within function).
③ form parameters.
Static storage:
① static local variables (valid within functions).
② static external variables (valid in this program file).
③ external variables (the entire program can be referenced).
5. Static and dynamic storage areas
Variable storage can be distinguished from static storage and dynamic storage in a location where the value of the variable is stored:
In-memory static storage:
① 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.

The use of C-language static

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: 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.