C-language static variables and static functions __c++

Source: Internet
Author: User
1. Static variable
The type descriptor for a static variable is static. Static variables of course belong to static storage, but the amount of static storage is not necessarily static variables. For example, external variables are static storage, but not necessarily static variables, which must be defined by static to be static external variables, or static global variables.
2. Static local Variables
Static local variables, which are static storage, have the following characteristics:
(1) A static local variable defines its lifetime as the entire source program within a function, but its scope remains the same as an automatic variable, which can only be used within the function that defines the variable. After exiting the function, it cannot be used, although it continues to exist.
(2) Allow initial values such as arrays to be assigned to static local quantities of the constructed class, and the system automatically assigns a value of 0 if the initial value is not assigned.
(3) If the static local variable of the basic type is not assigned an initial value in the description, the system automatically assigns 0 values. For an automatic variable, the value is indeterminate. According to the characteristics of static local variables, it can be seen that it is a lifetime of the entire source program. Although it cannot be used after leaving the function that defines it, it can continue to work when the function that defines it is invoked again, and the value left after the previous call is saved. Therefore, you can consider static local variables when you call a function multiple times and require the values of certain variables to be preserved between calls. Although the above goal can be achieved with global variables, global variables sometimes cause unexpected side effects, so it is advisable to adopt local static variables.
3. Static global variables
The description of the global variable (external variable) before being labeled static constitutes a static global variable. The global variable itself is the static storage mode, static global variables are of course also static storage mode. The two are not different in the way they are stored.  The difference between the two is that the scope of the Non-static global variable is the entire source program, and when a source program consists of multiple source files, non-static Global variables are valid in each source file. A static global variable restricts its scope, that is, it is only valid within the source file that defines the variable, and it cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can only be common to functions within that source file, so it is possible to avoid causing errors in other source files. From the above analysis, we can see that the change of the local variable to the static variable is the change of its storage mode that changes its lifetime. Changing the global variable to a static variable changes its scope and limits its use. So the static this specifier plays a different role in different places.
4. Static function ...
internal functions and external functions
When a source program is composed of multiple source files, C language can be divided into internal functions and external functions depending on whether the function is called by functions in other source files.
1 internal functions (also called static functions)
A function defined in a source file can only be called by a function in this file, not by a function in the other file of the same program, which is called an intrinsic function.
To define an intrinsic function, simply add a "static" keyword before the function type, as follows:
static function type function name (function parameter table)
{......}
The keyword "static", translated into Chinese is "still", so the internal function is also called static function. But the meaning of "static" here is not the way of storage, 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, they do not have to worry about the functions they define, whether they will have the same name as the functions in other files, because the same name does not matter.
2 external functions
Definition of an external function: When a function is defined, it is an external function if the keyword "static" is not added, or the word "extern" is prefixed:
[extern] Function type function name (function parameter table)
{......}
When you call 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 (...)/* defines an external function * *
{......}
(3) file subf2.c
......
extern void process (...)/* Define external function * *
{......}
(4) File subf3.c
......
extern void output (...)/* Define external function * *
{......}

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.