寄存器变量告诉编译器将变量存储在CPU寄存器中,而不是内存中。经常使用的变量被保留在寄存器中,它们具有更快的可访问性。我们永远无法获取这些变量的地址。使用"register"关键字声明寄存器变量。
作用域 - 它们是局部的。
默认值 - 默认初始化值是垃圾值。
生命周期 - 在定义它的块的执行结束之前。
立即学习“C语言免费学习笔记(深入)”;
以下是C语言中寄存器变量的示例:
演示
#include <stdio.h> int main() { register char x = 'S'; register int a = 10; auto int b = 8; printf("The value of register variable b : %c</p><p>",x); printf("The sum of auto and register variable : %d",(a+b)); return 0; }
The value of register variable b : S The sum of auto and register variable : 18
Register关键字也可以与指针一起使用。它可以拥有内存位置的地址。它不会产生任何错误。
下面是C语言中register关键字的一个示例
实时演示
#include<stdio.h> int main() { int i = 10; register int *a = &i; printf("The value of pointer : %d", *a); getchar(); return 0; }
The value of pointer : 10
以上就是在C语言中,“register”关键字的详细内容,更多请关注php中文网其它相关文章!
C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号