Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore
C language has rich data types and operators, so the computing power is very powerful. The values used in the calculation process are generally stored using variables. Variables also have different types. Different types of variables have different storage types, different lifecycles, and different scopes, the C language also provides some keywords to set attributes of variables (such as storage type and lifecycle ).
I. Scope of Variables
C language divides variables into local variables and global variables based on different variable scopes.
1. Local Variables
1> definition: a variable defined within a function is called a local variable. The format parameter is also a local variable.
2> scope: A local variable is valid only within the function that defines it. That is, a local variable is used only within the function that defines it. Other functions cannot use it.
2. Global Variables
1> definition: variables defined outside all functions are called global variables.
2> scope: the scope of a global variable ranges from the position of the defined variable to the end of the source program. That is, the global variable can be shared by other functions after the defined position.
1 int a;2 3 int main ()4 {5 int b;6 return 0;7 }
The variable a in the first row is a global variable, and the variable B in the second row is a local variable.
Ii. Storage types of Variables
* The storage type of variables refers to where the variables are stored. There are three places for storing variables: common memory, runtime stack, and hardware register. The storage type of a variable determines when the variable is created, when it is destroyed, and how long its value can be maintained, that is, the life cycle of the variable.
* The C language divides variables into automatic variables, static variables, and register variables based on their storage types.
1. Automatic Variables
1> definition: Automatic variables are stored in the stack.
2> what are automatic variables? The local variables modified by the keyword auto are all automatic variables. However, this keyword is rarely used, which is basically useless, because all local variables are automatic variables by default.
3> life cycle: the automatic variable is created only when the program executes the code block (function) that declares the automatic variable. After the code block (function) of the automatic variable is executed, these automatic variables are automatically destroyed. If a function is called repeatedly, these automatic variables are re-created each time.
1 void test(int a, int b) {2 int c = a + b;3 4 auto int d;5 }
The variables a and B in the first row, the variables c in the second row, and the variables d in the second row are all automatic variables.
2. Static variables
1> definition: static variables are stored in static memory, that is, they do not belong to stacks.
2> what are static variables:
- All global variables are static variables.
- The local variable modified by the keyword static is also a static variable.
3> lifecycle: a static variable is created before the program runs. It always exists throughout the entire running period until the program ends.
1 # include <stdio. h> 2 3 int a; 4 5 void test () {6 static int B = 0; 7 B ++; 8 9 int c = 0; 10 c ++; 11 12 printf ("B = % d, c = % d \ n", B, c); 13} 14 15 int main () {16 int I; 17 // call test function 18 for (I = 0; I <3; I ++) {19 test (); 20} 21 22 return 0; 23} three times in a row}
* The variables a and B in the 3rd rows are static variables. The variables c in the 6th rows and I in the 9th rows are automatic variables.
* Because the variable B in the first row is a static variable, it will be created only once and its lifecycle will continue until the end of the program. Because it will only be created once, the Code in line 6th will only be executed once. The value of variable B will not be reinitialized to 0 when the test function is called again.
* Note: although the variable B of row 6th is a static variable, it only changes its storage type (that is, its lifecycle) and does not change its scope, variable B can only be used within the test function.
* The test function is repeatedly called three times in the main function. The output result is:
3. register variables
1> definition: variables stored in hardware registers are called register variables. Register variables are more efficient than variables stored in the memory (by default, both automatic variables and static variables are stored in the memory)
2> which variables are register variables:
- The automatic variables modified by the keyword register are register variables.
- Only automatic variables can be register variables, but not global variables and static local variables.
- Register variables are only applicable to int, char, and pointer type variables.
3> lifecycle: Because the register variable itself is an automatic variable, the register variable in the function occupies the value stored in the register when calling the function. When the function ends, the register is released and the variable disappears.
4> note:
- Due to the limited number of registers in the computer, too many register variables cannot be used. If the register is saturated, the program automatically converts the register variable to automatic variable processing.
- To increase the computing speed, some frequently used automatic variables are usually defined as register variables, so that the program allocates registers for it as much as possible, instead of memory.
1 int main() {2 register int a;3 return 0;4 }
Variable a in Row 3 is a register variable.