在C编程语言中有四个存储类,分别是:
- auto
- extern
- static
- register
寄存器变量
-
关键字是register。
-
寄存器变量的值存储在CPU的寄存器中,而不是存储在内存中,普通变量存储在内存中。
-
寄存器是CPU中的临时存储单元。
-
它们允许寄存器变量比普通变量具有更快的访问时间。
示例1
以下是C程序的register存储类:
演示
#include<stdio.h>
main ( ){
register int i;
for (i=1; i<=5; i++)
printf ("%d ",i);
}
Output
The output is stated below −
1 2 3 4 5
Example 2
考虑另一个使用寄存器存储类的C程序 −
在线演示
#include<stdio.h>
int main(){
register int a;
printf("%d",a); //prints default value of a =0
}
Output
The output is stated below −
0
Example 3
以下是用于静态存储类的第三个C程序 −
#include<stdio.h>
int main(){
register int i = 10;
int *p;
//int *p = &i; //error occurred ,here we are trying to request address of register variable
printf("Value of i: %d", *p);
printf("Address of i: %u", p);
}
Output
The output is stated below −
Error:add of reg var?