Highlight:
1. Variables
2. Distribution of C Programs in memory
3. Modify the keyword of a Variable
1. Variables
----> Local variables
Lifecycle: from definition to function completion
Scope: Within the function that defines this local variable
----> Static local variables
Life cycle: entire program
Scope: defines the function of the local variable.
Differences between static local variables and local variables
1 #include <stdio.h> 2 test(){ 3 int j=0; 4 j++; 5 printf("j=%d\n",j); 6 } 7 8 test1(){ 9 static int jj=0; 10 jj++; 11 printf("jj=%d\n",jj); 12 } 13 14 int main() 15 { 16 int i=0; 17 for(i=0;i<5;i++){ 18 test(); 19 } 20 for(i=0;i<5;i++){ 21 test1(); 22 } 23 return 0; 24 }
In comparison, we can see the characteristics of local variables modified using static. The access range is the same as that of common local variables, but the value will not be released after the function is executed.
Local variables plus static ------> extended scope will not be recycled because the function returns
The global variable is added with static ------> the restriction scope is in the current source file.
----> Global variables
Life cycle: within the entire declared life cycle of the program
Scope: the entire program Scope
Differences in Initialization of local variables: local variables are random data without initialization, and the value of the global variables without Initialization is 0.
Use global variables to simulate stack operations:
1 #include <stdio.h> 2 #include <stdbool.h> 3 4 int INDEX; 5 int data[20]; 6 7 bool push(int d){ 8 if(INDEX>=20) 9 return false; 10 data[INDEX++]=d; 11 return true; 12 } 13 14 int pop(){ 15 return data[--INDEX]; 16 } 17 int peak(){ 18 return data[INDEX-1]; 19 } 20 21 bool empty(){ 22 if(INDEX<=0) 23 return true; 24 return false; 25 } 26 27 int main(){ 28 int i=0; 29 for(i=0;i<10;i++) 30 { 31 push(i+1); 32 } 33 34 printf("pop data is %d\n",pop()); 35 printf("pop data is %d\n",pop()); 36 printf("pop data is %d\n",pop()); 37 printf("show the peak %d\n",peak()); 38 printf("pop data is %d\n",pop()); 39 printf("now %s\n",empty()?"empty":"not empty"); 40 return 0;
----> Local priority of Variables
Local priority: if the global variable has the same name as the local variable block variable, block variables take precedence over block variables within the block variable access range. Local variables take precedence over block variables within the local variable access range.
1 #include <stdio.h> 2 #include <time.h> 3 4 int main() 5 { 6 int h=0,m=0,s=0; 7 int t=0; 8 while(1) 9 { 10 t = time(0);//get the current time; 11 s=t%60; 12 m=t%3600/60; 13 h=(t%(24*3600)/3600+8)%24; 14 printf("%02d:%02d:%02d\r",h,m,s); //"......\r" 15 fflush(stdout); // fflush 16 while(t==time(0)); 17 } 18 return 0; 19 }
2. Distribution of C Programs in memory
----> (1) code area (Text Segment): the area where the program code is stored. The program segment is read-only and the literal value is constant.
Code zone commands are executed in sequence according to the program design process. For ordered commands, they are executed only once (each process). If they are repeated, you need to use Jump commands. If recursion is performed, stack is required.
----> (2) initialize the global data zone/static data zone (data segment ). Only initialize once.
----> (3) Data zone not initialized (BSS ). Change the value at runtime.
BSS usually stores a memory area of the global variables and static variables that are not initialized in the program.
----> (4) heap ). Used for dynamic memory allocation.
Application: malloc remalloc new oC (alloc new init)
Release: Free Delete oC (Release)
Heap operations: the object is too large, and the number and size of objects required by the program are unknown in advance.
----> (5) stack area ). Saved local variables temporarily created in the program
Stack is usually stored: local temporary variables are automatically allocated at the beginning of the program, and the memory is automatically released at the end of the program. The returned pointer of the storage function
3. Modify the keyword of a Variable
------------- + --------- + -------------------
| Scope | visibility
------------- + --------- + -------------------
Local variable | function/block
Static local variable | process | function/block
Global variable | process | all files (external variables)
Static global variable | process | definition file
------------- + --------- + -------------------
Global function | process | all files
Static global function | process | definition file
------------- + --------- + -------------------
---> Static local variables and global variables:
(1) Static local variables are defined in the function, but they do not exist when called as automatic variables, and disappear when you exit the function. Static local variables always exist, that is, their lifetime is the entire source program.
(2) Although the lifetime of a static local variable is the entire source program, it can only be used within the function defining the variable. After exiting the function, although the variable still exists, it cannot be used.
(3) If an initial value is not assigned to a static local variable of the basic type, the system automatically assigns the value 0. If the initial value is not assigned to the automatic variable, the value is not fixed.
(4) According to the characteristics of static local variables, it can be seen that it is a kind of survival for the entire source file. Although this function cannot be used after it is defined, it can continue to be used when the function is called again, and the value left after the previous call is saved. Therefore, when you call a function multiple times and require that the values of some variables be retained between calls, you can consider using static local variables.
(5) The difference between global variables is that global variables can be used by all functions after they are defined, but static local variables can only be used in one function.
----> Const: Read-Only variable, which cannot be modified
----> Register
----> Volatile
Function address --- print function name