[My C language interview series] 002 Summary of local variables and global variables?

Source: Internet
Author: User
[My C language interview series] 002 Summary of local variables and global variables?

Summary of local variables and global variables

 

Local variable

Local variables are also called internal variables. Local variables are defined in the function.Its scope is limited to internal functions.It is invalid to use this variable after leaving the function.

Local variables can be divided into dynamic and static storage types.

Local variables of the dynamic storage type are dynamically allocated storage space, and data is stored in the dynamic storage area (stack. The function is automatically released after the function call ends. The lifetime is the execution process of the function that declares the variable.

Local variables of the static storage type are static storage space allocated, and data is stored in the static storage area. The program is not released during the entire running period, and the lifetime runs throughout the entire process of running the program.

If the local variables in the function are not specifically declared as static storage classes, the storage space is dynamically allocated by default. Auto is omitted by default during variable declaration.

Global Variables

Global variables, also known as external variables, are defined outside the function.The scope starts from the definition of the variable to the end of the program file.. Global variables are all stored in the static storage area. When the program starts to execute, the global variables are allocated to the storage area, and the program is released after the row is complete. They occupy a fixed storage unit during program execution without Dynamic Allocation and release;

If the external variable is not defined at the beginning of the file,Its effective scope is only limited to the definition at the end of the file.

If the function before the definition point wants to reference this external variable, you should use the keyword extern to declare this variable before the reference ". This variable is an external variable that has been defined. With this declaration, you can legally use this external variable from the "Declaration.Its effective scope is extended from this fileExternDeclaration ends at the end of the file.

If the keyword static is added before the global variable declaration, other files cannot access and use the variable,Its effective scope is only limited to the definition at the end of the file.

 

Can a local variable be renamed with a global variable?

Local variables can be renamed with global variables, but local variables block global variables. When this variable is referenced in a function, local variables with the same name are used instead of global variables.

PS:This pairExternThe declared global variables are the same..

 

 

Related Article

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.