C language, Static

Source: Internet
Author: User

  TerminologyStatichas an unusual history..at first, inCkeyword introduced inStaticis to represent a local variable that still exists after exiting a block. Subsequently,Staticin theChas a second meaning: global variables and functions that represent the inability to be accessed by other files. To avoid introducing new keywords, you are still using theStatickeyword to indicate this second meaning. Finally,C + +reuse this keyword and give it a different third meaning from the previous one: variables and functions that represent any particular object belonging to a class other than this class(with theJavaThe meaning of this keyword is the same).

    • Static variable:
Static local variable:

The local variable is defined as a local static variable by adding the keyword static before the local variable.
1) in-memory location: Static Storage Area
2) Initialization: Uninitialized global static variables are automatically initialized by the program to 0 (the value of the automatic object is arbitrary, except that it has been explicitly initialized)
3) Scope: The scope is still a local scope, and when the function or statement block defining it ends, the scope ends.

When static is used to modify local variables,

    it changes the location where the local variables are stored : from the original stack to the static storage area.

    changed initialization mode : Static local variable is initialized only once, next time based on last result value.

changed the life cycle: Local static variables are not destroyed after they leave the scope, but still reside in memory until the program ends.

Static global variable:

The global variable is defined as a global static variable by adding the keyword static before the global variable.
1) in-memory location: static storage (static storage is present during the entire program run)
2) Initialize: Uninitialized global static variable is automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless he is displayed initialized)
3) Scope: Global static variables are not visible outside the declaration of his file. Start with the definition exactly to the end of the file.

Global global variables are statically stored in their own right, and static global variables are, of course, stored in a static manner. The difference is that a static global variable restricts its scope and is valid only within the source file that defines the variable.

    • static function:

can only be called by a function in this file. The difference between the static function and the normal function:The static function has only one copy in memory, and the normal function maintains a copy of each call .

The benefits of using static functions in the C language:

  When different people write different functions, they don't have to worry about the functions they define, and whether they have the same name as the functions in other files.

Static functions are automatically assigned to a storage area that has been in use until the application instance is exited, avoiding the pressure stack out of the calling function, which is much faster.

C++:

Reference:

1.the role of staticHttp://www.cnblogs.com/dc10101/archive/2007/08/22/865556.html

2. The static keyword is explained http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777441.html

C language, Static

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.