Global variables, static global variables, static local variables, and local variables

Source: Internet
Author: User

Both global and static variables are in the heap.

The scope of global variables is the wholeProgram(If the program is multiple files, it must be described in other files ).
The scope of a static variable depends on the position of the static variable. If it is in a function, the scope is the function.

Static global variables, which can be used only in this file.

A global variable is an external variable that does not define the storage type. Its scope is from the definition point to the end of the program. If the storage type character is omitted, the system will default to the automatic type.

Static global variables are external variables that define the storage type as static. Their scope is from the definition point to the end of the program. The difference is that the storage type determines the storage location, static variables are stored in the data area of the memory. They are allocated fixed bytes before the program starts running. The size of allocated bytes remains unchanged during the program running. the occupied memory is released only after the program runs.

Automatic variables are stored in the stack area. The stack area is also a part of the memory, which is used repeatedly when the program is running.

Global variables, static global variables, and static local variables are stored in the static storage area of the memory, and local variables are stored in the stack area of the memory.

By scope, global variables are valid throughout the project file; static global variables are valid only in the file that defines them; static local variables are valid only in the function that defines them, only the program allocates memory once. After the function returns, the variable does not disappear. The local variable is valid in the function defined by the function, but fails after the function returns.

If the global variables and static variables are not manually initialized, the compiler initializes them to 0. The value of the local variable is unknown.

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.