Global variables and local variables, the role of the static keyword

Source: Internet
Author: User

Global variables and local variables

Global Variables : variables defined outside the function become global variables; The global variable allocates storage in static storage Hopefully, the function in the file can call the global variable if it is not freed during the program's run, and the function in the other file calls the global variable, and the extern declaration is required.

Static global Variables : When you define a variable outside of a function, the variable is a static global variable if you add a static declaration.

local variable : A variable defined in a function or compound statement that allocates a storage unit in a dynamic store, dynamically allocated at invocation, and disposed automatically at the end of a function or compound statement.

static local Variables : When you define a local variable in a function, the variable is a static local variable if you add the STATCI declaration.

the difference between a local variable and a global variable: A global variable is a variable that is valid for the entire source program, and the local variable dies at the end of the function or loop to which it belongs.

The role of the keyword static

1. In the body of a function , a variable declared as static maintains its value in the body of the function during invocation (the variable is stored in the static variable area)

2. within the module (but outside the function body), a variable declared as static can be accessed by a function within the module, but cannot be accessed by other functions outside the module, which is a local global variable

3. within the module , a function declared as static can only be called by other functions within this module. That is, the function is limited to the local scope of the module that declares it.

Global variables and local variables, the role of the static keyword

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.