在C中有四种存储指示器,下文一一介绍每种指示器的作用:
Auto指示器:
- Default initial value is garbage (computer generated) 默认值是随机的无意义的
-
Scope is local to the block in which declared.作用于限于声明变量的块中
-
Life is till control remains within the block.程序块控制其生命周期
-
Stored in the computer's memory (RAM) 存储于内存中,其实是在栈里面
-
Memory is economically used.块结束将自动释放内存
-
Built is protection as they are local.只在声明块里面有效
-
They do not retain value once control has come out from the block. Each time control is entered into the block a new variable is created and the same dies when control exits. 出了作用域就不能存取数据了
-
They are created on stack. 变量存储在栈中
Static指示器:
- Default initial value is zero.默认值是0
-
Storage is memory (in the stack).存储于栈中
-
Internal static have no linkage. 内部静态变量无耦合(意思是独立的,和其他任何地方都无关系)
-
External static have internal linkage.外部静态变量有耦合(意思是耦合的,在外部某个地方一定已经有定义过该变量)
-
Life till program execution is not over. 程序不止,生命不止,当然仅仅限于当前文件有效。 也就是说即使全局静态变量也不能被其他文件访问到。
-
Static variable are initialized only once.静态变量只会初始化一次
-
Static variables are initialized before the execution of program begins thus they are called load time variable.静态变量是在程序执行之前完成初始化,所以叫加载时变量。
-
They retain the value between different function calls.它会随着function里面相关的操作改变存储的值。
Register指示器:
- Variable are stored within CPU registers. 变量请求存储于寄存器
-
Writing register storage class is simply a request to the compiler so variable may not be stored in CPU registers if they are not free/available.只是请求,未必最后存储寄存器
-
You will never come to know whether variables are stored within register or within memory.没法知道它最后是否存储在寄存器还是在内存
-
Number of registers is limited by the type of CPU.随着CUP型号的不同,内部寄存器的数量也是不同的有限的
-
They are usually limited to the char and int type variables though some compiler permits other type also.往往只有整型和字符型变量可能被存储在寄存器,其他类型一般不支持
-
Default initial value is garbage.默认值没有意义
-
Scope is local to the block in which declared.只在声明块中有效
-
Life is block only.块执行完,变量释放,该关键字不能用来声明全局变量
-
You can not take address of a register class type variable.不能进行取地址操作
Extern指示器:
- Default initial value is zero.默认值是0
-
Scope is global from the point of definition to the rest of program or we can say they have file scope.作用范围限于该文件内,不能跨文件
-
Extern keyword is used when a variable is declared and tells the compiler about the type of identifier.常常用于表明变量的作用域
-
The definition is outside the outermost block and it allocates memory storage.定义必须位于所有块之外
-
The use of external variables provides a way so that information can be shared among number of functions.表明该变量可以被同一文件的函数共享
-
The use of extern keyword is optional as external variables are recognized by the location of their definition within the program.关键字EXTERN可以随意使用,只要能该变量有在program中作为全局变量定义过
-
An external variable declaration cannot include the assignment of initial values.声明不能给初始值
-
Persistent storage of a variable retains the current value.该变量存储当前的值
-
The value is globally available.该变量的值在program范围内有效