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

📅  最后修改于: 2021-09-15 01:29:07             🧑  作者: 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 基础课程。