Scope of C language (iv) variables

Source: Internet
Author: User

The scope of a variableThe C language divides variables into local variables and global variables, depending on the scope of the variables. 1. local variable 1) Definition: A function defined inside a function, called a local variable. Formal parameters also belong to local variables. 2) Scope: A local variable is only valid within the function that defines it, that is, the local variable is used only within the function that defines it, and other functions cannot use it.
2. Global variable 1) definition: A variable defined outside all functions, called a global variable.

2) Scope: The scope of a global variable is to start from the location where the variable is defined to the end of the source program, that is, the global variable can be shared after its defined position.


Second, the storage type of the variablethe variable storage type is where the variable is stored.
There are three places where you can store variables: normal memory, runtime stacks, hardware registers.
the storage type of a variable determines when a variable is created, when it is destroyed, that is, the declaration period of the variable.
The C language is divided into: automatic variable, static variable, register variable according to the storage type of the variable.
1. Automatic variables
void Test (int a,int b) {int c =a+b;auto int D;}
1) Definition: The automatic variable is stored in the stack. 2) which are auto variables: Auto keyword-Modified local variables, but basically obsolete, because all local variables are automatically variable by default. 3) Life cycle:Automatic variables are created when the program executes to a code block (function) that declares an automatic variable, and the variables are automatically destroyed when the code block (function) where the automatic variable resides is executed. If this function is called repeatedly, these automatic variables are recreated each time.
2.Static Variables
#include <stdio.h> #include "one.h" #include "two.h" int A;//static variable void Test2 () {static int b=0;//static variable b++;} int main () {test2 ();p rintf ("%d\n", b); return 0;}
1) Definition: Static variables are stored in static memory, that is, not on the stack.2) which are static variablesAll global variables are static variables Local variables modified by the static keyword are also 3) Life cycle a static variable is created before the program runs, and persists throughout the program's run until the program ends. 4) Understanding of the life cycle and scope the above variable A, when the code executes to that row, creates a static variable that is stored in static memory. scope: After the line code. Life cycle: During the entire program run. the variable b is defined inside the function, is created when the function is first called, stored in static memory, and the second time the function is called, the row is not executed. Scope: Inside the function. Life cycle: During the entire program run.
3. Register variables
1) Definition: A variable stored in a hardware register, called a "register variable". Registers are more efficient for accessing variables stored in memory. By default: Both automatic and static variables are stored in memory, but static variables are stored in static memory. 2) which variables are automatic variables modified by register variable register keyword. only automatic variables can be register variables. Register variables are limited to int, char, pointer. 3) life cycle because the register variable itself is an automatic variable, the register variable in the function-occupies the value stored in the register when the function is called, and the variable disappears when the function ends. 4) Use note that too many register scalars cannot be used because the number of registers in the computer is limited. If the register uses saturation, the program automatically converts the register variable to automatic variable handling.

Summary:    by scope: Global variables and local variables
Global variables: variables declared outside of a functionLocal Variables: function interior and function parameters
by storage location: Automatic variable, static variable, register variable    
Automatic variables: all local variables
Static variables: Global variables and static modified local variables
Register Variable: Register-Modified local variable.





















Scope of C language (iv) variables

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.