Static functions and variables

Source: Internet
Author: User
Static
Declared variables have two features in the C language:
1) The variables will be placed in the global storage area of the program, so that the original values can be maintained during the next call. This is the difference between stack variables and heap variables.
2) The variable uses static to notify the compiler that it is only visible within the scope of the variable. This is the difference between it and global variables.
TIPS:
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.

Add static before the function to make the function a static function. However, the meaning of "static" here is not the storage method, but the scope of the function is limited to this file (so it is also called an internal function ). The advantage of using internal functions is that when different people write different functions, you don't have to worry about your own defined functions and whether they will have the same name as the functions in other files.

Extended analysis: the term static has an unusual history. At first, the keyword static was introduced in C to indicate that a local variable still exists after exiting a block. Then, static
C has a second meaning: it is used to indicate global variables and functions that cannot be accessed by other files. To avoid introducing new keywords, the static keyword is still used to indicate the second meaning. Finally,
C ++ reuses this keyword and gives it a third meaning different from the previous one: variables and functions that belong to a class rather than any specific objects of this class

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.