📜  C中静态变量和寄存器变量之间的区别

📅  最后修改于: 2021-05-25 19:43:40             🧑  作者: Mango

静态变量

静态变量具有保留其值的属性,即使它们超出其范围也是如此!因此,静态变量在其先前的作用域中保留其先前的值,而不会在新的作用域中再次初始化。

句法:

static data_type var_name = var_value;

注册变量

寄存器比内存访问要快,因此可以使用register关键字将C程序中最常用的变量放入寄存器中。关键字register会向编译器提示可以将给定的变量放入寄存器中。编译器可以选择是否将其保存在寄存器中。通常,编译器自己进行优化并将变量放入寄存器中。

句法:

register data_type var_name = var_value;

C中静态变量和寄存器变量之间的差异。

Static Variables Register Variables
Keyword used is – “static”. Keyword used is – “register”.
Static variable may be internal or external depending on the place of declaration. Register variables are declared inside a function.
Internal static variables are similar to auto variables or local variables. Whereas, external static variables are similar to global variables. Register variables are similar to auto or local or internal variables.
The execution speed is slower than register variables. The register variables leads to faster execution of programs.
Internal static variables are active(visibility) in the particular function and External static variables are active in the entire program. Register variables are active only within the function.
Internal static variables are alive(lifetime) in until the end of the function and External static variables are alive in the entire program. Register variables are alive until the end of a function.
Static variables stored in initialized data segments. Register variables are stored in registers.
Static variable is stored in the memory of the data segment. In register variables, CPU itself stores the data and access quickly.
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。