C Language Local Variables & global variables & variable storage methods

Source: Internet
Author: User

One, Angle analysis:

Variable scope-------> Local variables and global variables

Lifetime of variable existence---------> static storage mode and dynamic storage mode

Second, local variables:

Local variables are also known as internal variables. A local variable is defined within a function as a description. its scope is limited to functions , and it is illegal to use the variable after leaving the function.

Example:

The scope of the local variable also describes the following points:

1) The main function is also a function, which is parallel to other functions. variables defined in the main function can also be used only in the main function, not in other functions. Also, variables defined in other functions cannot be used in the main function.

2) parameter variables are local variables that belong to the tuned function, and argument variables are local variables that belong to the keynote function.

3) The same variable names can be used in different functions, they represent different objects, the compiler allocates different units and does not interfere with each other.

4) variables defined in a compound statement can be scoped only within a compound statement.

Example:

Third, global variables

Global variables, also known as external variables, are variables defined in the foreign language of the function. It does not belong to a function, it belongs to a source program file (so as long as it is in the same source program file, then you can use this variable for assignment or other operations). Its scope is the entire source program (how to understand the "source program" here). the use of global variables in functions should generally be described as global variables. Only global variables that are described within a function can be used. The description of the global variable is appended to: extern. But a global variable defined before a function is used within that function and can no longer be described.

For example:

Three, the storage mode of the variable

The user storage space can be divided into three parts:

Program Area
Static storage (How to allocate a fixed amount of access space during a program run.) )

Dynamic storage (the way in which storage space is dynamically allocated as needed during the program's run.) )

Examples of dynamic storage methods:

Additional Links:

1, the difference between the global variables and local variables in memory in detail parsing http://www.jb51.net/article/42012.htm

2.

Continue writing tomorrow.

-----------------

Hayder

E:[email protected]

2015-4-26

C Language Local Variables & global variables & variable storage methods

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.