C language 18-variable type

Source: Internet
Author: User

    • The scope of a variable
    • Second, the storage type of the variable

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore.

The C language has rich data types and operators, so the computational power is very powerful, and the values used in the calculation are generally stored in variables. Variables are also classified, different types of variables have different storage types, different lifecycles, different scopes, and C also provides keywords to set the properties of variables (such as setting the storage type, life cycle).

The scope of a variable

The C language divides variables into local variables and global variables, depending on the scope of the variables.

1. Local Variables

1> definition: A variable 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 variables

1> definition: A variable defined outside all functions, called a global variable.

2> scope: The scope of a global variable is from the location where the variable is defined to the end of the source program, where the global variable can be shared by other functions after its defined position.

1 int a;2 3 int main () 4 {5     int b;6     return 0;7}

The variable A in line 1th is a global variable, and the variable B of row 5th is a local variable.

Second, the storage type of the variable

* The storage type of a variable is where the variable is stored. There are 3 places you can use to store variables: normal memory, runtime stacks, hardware registers. The storage type of a variable determines when a variable is created, when it is destroyed, and how long its value remains, which determines the life cycle of the variable.

* C language depending on the storage type of the variable, variables can be divided into: automatic variables, static variables, register variables.

1. Automatic variables

1> definition: An automatic variable is stored in the stack.

2> which are auto variables: Local variables that are modified by the keyword auto are automatic variables, but rarely use this keyword, which is 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 automatic variables are destroyed on their own when the Code block (function) where the automatic variable is executed is completed. If a function is called repeatedly, these automatic variables are recreated each time.

1 void Test (int a, int b) {2     int c = A + b;3     4     auto int d;5}

Variables A, B, row 2nd of the variable C, row 4th of the variable d are the automatic variables in line 1th.

2. Static variables

1> definition: Static variables are stored in static memory, that is, not on the stack.

2> which are static variables:

    • All global variables are static variables
    • Static variable is also a local variable modified by the keyword static

3> life cycle: Static variables are created before the program runs, and persist throughout the program's run until the end of the program.

1 #include <stdio.h> 2  3 int A; 4  5 void Test () {6     static int b = 0; 7     b++; 8      9     int c = 0;1 0     c++;11     printf ("b=%d, c=%d \ n", B, c);}14 int main () {+     int i;17     //Call 3 times test function 18
   
    for (i = 0; i<3; i++) {         test ();     }21     return 0;23}
   

* The variable B of the variable A and line 6th in line 3rd is a static variable, and the variable C and 16th row variables I of line 9th are the automatic variables.

* Because the variable B of line 6th is a static variable, it is created only once and the life cycle continues until the end of the program. Because it is created only once, the 6th line of code executes only once, and the value of variable B is not reinitialized to 0 the next time the test function is called.

Note: Although variable B in line 6th is a static variable, it only changes its storage type (that is, the life cycle) and does not change its scope, and variable B can only be used inside the test function.

* We repeatedly call the test function in the main function 3 times, the output is:

3. Register variables

1> definition: A variable stored in a hardware register, called a register variable. Register variables are more efficient to access than stored in-memory variables (by default, both automatic and static variables are placed in memory)

2> which variables are register variables:

    • Automatic variables that are modified by the key register are register variables
    • Only automatic variables can be register variables, global variables and static local variables No
    • Register variables are limited to int, char, and pointer type variables

3> life cycle: Because the register variable itself is an automatic variable, the register variable in the function consumes the value stored in the register when the function is called, and the variable disappears when the function ends.

4> Use Note:

    • Due to the limited number of registers in the computer, too many register variables cannot be used. If the register uses saturation, the program automatically converts the register variable to automatic variable handling
    • To increase the speed of operations, it is common to define a number of frequently used automatic variables as register variables, so that the program allocates as many registers as possible, instead of memory
1 int Main () {2     register int a;3     return 0;4}

The variable A in line 2nd is a register variable.

C language 18-variable type

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.