There are two types of data in the C language: data types and Storage categories. The data type defines the data format (length), and the storage category defines the scope and lifetime of the data.
1. Declaration of variables
1.1
The general form of the declaration of a variable: stores the class data type variable name, and the data type takes int as an example:
- Auto variable: Auto int i;//automatic variable is local variable
- Local variables: variables defined inside a function, local variables default storage categories are automatic variables
- external variable/global variable: A variable that is defined outside the function and does not indicate a storage class
- Static variables: static int i; register variable: rigister int i;
- Static local Variables
- Scope: From definition start to function end.
- lifetime: From the definition start to the end of the program execution .
- Static global variables
- Scope: From the definition start to the end of the source file. That is, the extern declaration cannot be used by other source files.
- Lifetime: From the definition start to the end of the program execution.
- Register variables: rigister int i; Register variables are stored in the CPU.
1.2
- Static allocation: Static storage of data, including static variables and external variables, to release the memory units occupied by the variables at the end of the program execution.
- Dynamic allocation: There is a dynamic store of data, including register variables and local variables, and the memory units occupied by the variables are released when the function is executed.
The role of 2.static
2.1static Acting on variables
- Change scope (static global variable)
- Change lifetime (static local variable)
2.2static Acting on functions
- Change function Scope: a function that is modified by static is called an intrinsic function and can only be used in its defined source file. The default value for a function that is not modified by static is extern, which is an external function that can be used by the program's odd Tan Yuan file.
Static and unnamed namespace in the 3.c++
Unnamed namespace is superior to static keyword, primarily because the keyword applies only to the static variables declarations and functions, not to the user-defined types.
Two properties of data in C language and the function of static