The difference between static global variable and ordinary global variable detailed analysis _c language

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 variable and ordinary local variable?
(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 static before it constitutes a 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.

(2) What is the difference between static local variable and ordinary local variable?
A :the static local variable is only initialized once, and the next one is based on the previous 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 internal functions (static), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, you should indicate in a header file that the source file to use these functions contains this 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, the ordinary function in each call to maintain a copy of the program's local variables exist in (stack), global variables exist in (static zone), dynamic request data exist in (heap).

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.