The difference between a static global variable and a normal global variable

Source: Internet
Author: User

(1) What is the difference between a static global variable and a normal global variable?
(2) What is the difference between static local variables and ordinary local variables?
(3) What is the difference between the static function and the normal function scope?
(4) What is the difference between a static function and a normal function?


(1) What is the difference between a static global variable and a normal global variable?
A: The description of the global variable is then static, which makes it a global variable. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the whole source program, 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, which is valid only within the source file that defines the variable, and 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 be common only for functions within that source file, so you avoid causing errors in other source files. From the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use.
(2) What is the difference between static local variables and ordinary local variables?
A: The static local variable is initialized only once, the next time based on the last result value;
(3) What is the difference between the static function and the normal function scope?
Answer: Only in this document. Functions that are used only in the current source file should be described as intrinsic (static) and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to use these functions should contain the header file.
(4) What is the difference between a static function and a normal function?
A: The static function has only one copy in memory, and the normal function maintains a copy of the program's local variables in the (stack) in each call, the global variable exists in the (static zone), and the dynamic requisition data is present in the (heap).

The difference between a static global variable and a normal global variable

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.