Variable Storage types in C language are divided into: 1 static storage
It can be divided into external variables (extern) and static variables (static ).
(1) static variables
When the variable is defined, the storage unit is allocated and remains unchanged until the entire program ends.
Format:
Static data type variable name
After a static variable is out of its scope, its storage space is retained, even if the function call ends, it is not released. That is to say, any function directly acts on the static variable.
During definition, You can initialize static variables in the following format:
Static type variable name = constant;
For example:
Static int A = 1;
Static char a [] = "this is a text ";
Instance 1 with the same name
/* Same name */# include <stdio. h> int F (); main () {int A, S; /* The defined s here is a function-level local variable */printf ("Same name/n"); s = 100; printf ("in the function main () before executing the loop statement, S = % d/N ", S); for (a = 1; A <5; A ++) {static int S = 10; /* The defined s here is a statement-level static variable */printf ("in the main () function, when the % d Round Robin, S = % d/N ", a, S );}}
Static variables keep their storage space during the program running. If too many static variables (especially static arrays) are defined in the program, the memory space may be insufficient.
(2) external variables
External variables are defined outside the function, that is, global variables. If you need to reference variables between multiple programs, you need external variables.
The format of external variables is described as follows:
Extern data type variable name;
Note: external variables can only be defined once, that is, only one bucket can be allocated. If you repeatedly define variables, an error will be reported during program compilation.
/* File tc1.c */int A;/* defines the external variable A */int f (); main (){... a = 1;/* call the external variable A */M = f ();}/* file tc2.c */extern int; /* indicates that A is an external variable */F (){... A ++ ;...}
Tc1.c and tc2.c are two different program files, but they all use the same variable A. Variable A is defined as an external variable and is defined in tc1.c, in the tc2.c file, A is an external variable.
Use the keyword extern to extend the variable scope:
Int F (); main () {extern int A;/* specifies the external variable */... a = 1;/* call the external variable */M = f ();} int A;/* define the external variable A */int f (){... A ++;/* call the external variable */...}
In the preceding program section, external variable A is defined after the main () function. To call it in main (), you must use the statement in main ().
"Extern int A" indicates that A is an external variable. Otherwise, if "int A" is used, only a local variable is obtained.
2. Dynamic Storage
It can be divided into automatic variables (auto) and register variables (register ).
(1) dynamic variables (auto)
In the process of program execution, you need to use it to allocate storage units to it, and release immediately after use.
The Definition Format of dynamic variables is as follows:
Auto data type variable name;/* the keyword auto can be omitted */
We usually see the form parameters and block variables are dynamic variables.
(2) register variables
Variable values are stored in the memory, and some variables may be used in large quantities (such as loop variables in for). To improve the execution efficiency, C language allows you to store the value of a variable in a CPU register, called a register variable.
The format of register variables is as follows:
Register data type variable name;
main(){ int a; register int i; for(i=1;i<1000;i++){ a+=i; } printf("%d",a);}
Only local variables can be defined as register variables. The number of register variables can be defined is limited. Generally, it is recommended to use 2 or 3,
Because the register variable is not in the memory, address calculation is not allowed. It is best to define the register variable in use and release it immediately after use.