This article is suitable for novices. It only talks about some features and usage methods, but does not explain the mechanism of stack, heap memory allocation and static storage area. Boss, please take a detour.
Code block: the part enclosed by braces {}, the loop body of the loop, and the execution body of the if.
Life cycle: as the name suggests, it is how long a variable can live. It refers to the process from the allocated memory to the destruction of variables.
Scope: in which code blocks the variable can work, these code blocks are the scope of the variable.
In C language, variables are classified according to different life cycles and scopes, including these four types: global variables, local variables, static local variables and static global variables.
Local variable: the scope is a variable of a local region. Out of scope, the variable will be destroyed and will no longer work. It is generally a variable declared inside a code block. For example, the following two pieces of code:
#include <stdio.h> int main() { { int a = 0, b = 1; } a = 3;/*Unable to access variables for internal code block*/ return 0; }
#include <stdio.h> void fun(); int main() { int a = 0, b = 1; return 0; } fun() {/*Variables from other code blocks cannot be accessed*/ a = 3, b = 5; }
When these two pieces of code are compiled, an error will be reported. The compiler prompts that a and b are not defined. Where a and b declare that they are in an independent code block, and external code blocks and other code blocks do not have access rights to a and b. And variables A and b will be destroyed after the code block is executed. The sub code block of the code block in which the variable is located can access the variable. For example, the following two sections of code
#include <stdio.h> int main() { int a = 0, b = 1; { a = 3, b = 5; } printf("%d %d", a, b); return 0; }
#include <stdio.h> int main() { int a = 0, b = 1; int i; for(i = 0; i < 3; i++) { a ++; b += 2; } printf("%d %d", a, b); return 0; }
Global variable: sometimes multiple functions or code blocks or multiple files need to share the same data. For a long time, there needs to be a variable that can be used by all functions. This variable is a global variable. The scope is global (the whole program), and the life cycle starts with the beginning of the program and ends with the end of the program. For example, the following code
#include <stdio.h> int a = 3, b = 4; void swap_a_b(); int main() { swap_a_b(); printf("%d %d", a, b); return 0; } void swap_a_b() { int temp = a; a = b; b = temp; }
The running result of this program will be 4 3. However, the following code compiler will not report an error (the running result is 3 4):
#include <stdio.h> int a , b; void swap_a_b(); int main() { a = 3, b = 4; swap_a_b(); printf("%d %d", a, b); return 0; } void swap_a_b() { int a , b; a = 4, b = 3; }
This is because C language code has the property of local variable priority when executing, swap_ a_ Although the names of a and B in the B () function and a and B outside are the same, they are not the same variable, and their addresses in memory are also different (equivalent to two people with the same name but different ID cards). swap_ a_ A and B in the B () function are local variables and the scope is swap_a_b(). Just like many novices will feel confused below
#include <stdio.h> int main() { int i = 1; while(i != 0) { int i; i= 0; }//This is an endless cycle return 0; }
This code will be an endless loop. Due to the local priority, the variable i declared inside the code block while will be given priority when used in the while loop body (if you have it, you don't need others). Although the scope of the variable i inside the code block main includes while, the priority is not as high as that inside the while. So the program will fall into an endless loop.
Static local variable: sometimes a function needs a value that can be saved without being destroyed. The value in it will remain unchanged during the next call. This variable is a static local variable. Its life cycle is the whole program, and its scope is the code block that locally declares it. Just add the static keyword in front of the declared variable. For example, the following code
#include <stdio.h> void PrintNum(); int main() { int i; for(i = 0; i < 6; i++) { PrintNum(); } return 0; } void PrintNum() { static int a = 0; printf("%d ", a); a++; }
The result of execution is 0 1 2 3 4 5. Local static variables cannot be accessed by code blocks other than local variables. When the code block is executed, the variables will not be destroyed, but "sleep".
Static global variable: add the static keyword before the global variable declaration. When declared as a static global variable, only the current file has permission to access the variable, and other external files do not have permission to access the current variable. This is used more in the actual development of multi file programming. Novices don't need in-depth understanding for the time being. Experts can bypass it.