Static and static functions in c

Source: Internet
Author: User

Static and static functions in c
 

 1. staticVariable
The type description of static variables is static. Static variables belong to static storage, but not necessarily static variables. For example, an external variable is a static storage method, but not necessarily a static variable. It must be defined by the static variable before it can become a static external variable or a static global variable.
 2.Static local variables
Static local variables are stored in static mode and have the following features:
(1) the lifetime of a static local variable is defined as the entire source program in the function, but its scope is still the same as that of an automatic variable. It can only be used within the function that defines the variable. After exiting the function, although the variable still exists, it cannot be used.
(2) An initial value such as an array can be assigned to the static local volume of the constructor class. If the initial value is not assigned, the system automatically assigns the value 0.
(3) If an initial value is not assigned to a static local variable of the basic type, the system automatically assigns the value 0. If the initial value is not assigned to the automatic variable, the value is not fixed. According to the characteristics of static local variables, it can be seen that it is a kind of lifetime for the entire source program. Although this function cannot be used after it is defined, it can continue to be used when the function is called again, and the value left after the previous call is saved. Therefore, when you call a function multiple times and require that the values of some variables be retained between calls, you can consider using static local variables. Although global variables can also achieve the above purpose, global variables sometimes cause unexpected side effects, so it is best to use local static variables.
 3.Static global variables
The description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided. From the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use. Therefore, the description of static plays different roles in different places.

Tip:

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

B. If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;

C. When designing and using functions that access dynamic global variables, static global variables, and static local variables, you must consider re-import;

D. If we need a reentrant function, we must avoid using static variables in the function (such a function is called a function with the "internal memory" function)

E. static variables must be used in a function. For example, if the return value of a function is of the pointer type, the address of the static local variable must be used as the return value. If the return value is of the auto type, the returned result is an error pointer.

 

Variables can be divided into global variables, static global variables, static local variables, and local variables.

Global variables, static global variables, and static local variables are stored in the static storage area of the memory, and local variables are stored in the stack area of the memory.

By scope, global variables are valid throughout the project file; static global variables are valid only in the file that defines them; static local variables are valid only in the function that defines them, only the program allocates memory once. After the function returns, the variable does not disappear. The local variable is valid in the function defined by the function, but fails after the function returns.


The description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.

From the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use.

 

 4. staticFunction.....
Internal and external functions
When a source program is composed of multiple source files, the C language divides the function into internal functions and external functions based on whether the function can be called by functions in other source files.
  1Internal functions (also known as static functions)
A function defined in a source file can only be called by functions in this file, but cannot be called by functions in other files of the same program. This function is called an internal function.
To define an internal function, you only need to add a "static" keyword before the function type, as shown below:
Static function name (function parameter table)
{......}
The keyword "static" is "static" in Chinese. Therefore, internal functions are also called static functions. However, the meaning of "static" here is not the storage method, but the scope of the function is limited to this file.
The advantage of using internal functions is that when different people write different functions, you don't have to worry about whether your own defined functions will have the same name as the functions in other files, because the same name does not matter.
  2External Functions
External Function Definition: when defining a function, if the keyword "static" is not added or the keyword "extern" is appended, this function is an external function:
[Extern] function name (function parameter table)
{......}
When calling an external function, you need to describe it:
[Extern] function name of the function type (parameter type table) [, function name 2 (parameter type table 2)…];
Static functions have different scopes than normal functions. Only in this file. Only functions used in the current source file should be declared as internal functions (static). Internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, it should be described in a header file that the source file to use these functions must contain this header file.

What is the difference between static global variables and common global variables? static global variables are only made once to prevent being referenced in other file units;
What is the difference between static local variables and common local variables: static local variables are initialized only once, and the next time is based on the previous result value;
What is the difference between a static function and a common function: a static function has only one copy in the memory, and a normal function maintains one copy in each call.
If the global variables and static variables are not manually initialized, the compiler initializes them to 0. The value of the local variable is unknown.

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.