There are four types of storage class Identifiers in C + +: Auto/static/register/extern
1.auto
Variables defined within a function or sub-program (including formal parameters) can be defined as auto (automatic variables). If you do not specify a storage class, it is implicitly defined as auto.
For example, a function class has the following definition:
auto int x, y;
Equivalent to:
int x, y;
2.static
In addition to formal parameters, you can define local variables and global variables as static variables. Use the static identifier.
static int a;//a is a global static variable
F ()
{static int b = 1;} b is a local static variable
Unlike auto, the lifetime of a static variable is when the program starts running to the end of the run (the auto variable lifetime is the end of the function call).
If the static variable is not assigned the initial value, the system automatically assigns the initial value 0; If the initial value is assigned, the initial value is assigned only once at compile time, and the variable is not assigned the initial value after the program is run. The value of the variable continues to be valid (the value held at the end of the last function call ) when the function where the local static variable was last called.
3.extern
A variable defined outside a function is an extern external variable if it is not declared with a static. External variables can only be implicitly defined as extern, and definitions cannot be displayed.
When declaring an external variable, the system does not allocate storage space.
#include <iostream>using namespacestd;intx =2, y =2;//define external variablesvoidF1 () {extern CharC1, C2;//DECLARE external variable cin>>c1>>C2;}CharC1, C2;//define external variablesintMain () {intm, N; F1 (); cout<<c1<<"+"<<c2<<"="<<x+y<<Endl; return 0;}
The result is this:
Note that if you remove the extern and then enter XY, the carriage return will appear:
Because the scope of C1,C2 is only inside the F1 function. If you do not use extern, want to achieve the first effect, as long as the
Char C1, C2;//define external variables
This sentence gets F1 () in front of the line.
4.register
The value of the register variable is stored in the registers of the CPU, and reads and writes faster. As the performance of computer hardware improves, register variables are now used less.
[C + +] Variable storage category