Static variable; static function

Source: Internet
Author: User

I. Static variables static variables are roughly divided into three usage types

 

1. It is used in local variables to become static local variables. Static local variables have two usages: Memory Function and global survival.

2. It is used for global variables and is mainly used to restrict the global variables from being called by other files.

3. Used for the member in the class. indicates that the member belongs to this class but does not belong to any specific object in the class.

 

1. Static local variables static local variables belong to the static storage mode, which has 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. Example: int fun () {static int A = 1; A ++;} variable A is initialized to 1 when it first enters this function! And then auto-increment 1. After entering this function, a will not be initialized again, and only perform auto-increment 1 operation. Before the static invention, the same function should be achieved, only global variables can be used: int A = 1; int fun () {A ++ ;}

 

2. Static global variables (external variables) are described as static global variables. 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.

 

3. The static keyword of the static class member variable has two meanings. You can check the context to determine

A: The variable is a static storage variable, indicating that the variable is stored in the static storage area.

B, indicates that the variable is an internal connection (in this case, the variable is not in any {}, just like the global variable, and static is added at this time), that is, in other. in the CPP file, the variable is invisible (you cannot use it ).

 

Ii. Static functions-internal functions 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.

 

1. Internal functions (also known as static functions) functions 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 type of 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 function external Function Definition: when defining a function, if the keyword "static" or the keyword "extern" is not added, this function is an external function: [extern] function name (function parameter table ){......} When calling an external function, you need to describe it: [extern] function type function name (parameter type table) [, function name 2 (parameter type table 2)…];

 

[Case] external function application.

(1) file mainf. C main () {extern void input (...), Process (...), Output (...); Input (...); Process (...); Output (...); }

(2) file subf1.c ...... Extern void input (......) /* Define external functions */{......}

(3) file subf2.c ...... Extern void process (......) /* Define external functions */{......}

(4) file subf3.c ...... Extern void output (......) /* Define external functions */{......}

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.