Static syntax [ZT] in C

Source: Internet
Author: User
1. static variable
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) Static local variables are defined in the function. Its lifetime is the whole sourceProgramBut its scope is still the same as that of the 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) allow initial values to be assigned to the static local volume of the constructor class. For example, if an array is not assigned an initial value, the system automatically assigns it a value of 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.

4. Static 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.
(1) internal 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 type 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.
(2) external 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)…];

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.