gpt4 book ai didi

c - 寄存器存储类的范围是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:15 24 4
gpt4 key购买 nike

我想知道每个存储类之间到底有什么区别。这里我使用了注册存储类。所以我很困惑,如果我们将 register 变量声明为全局变量,它的范围是什么? 在我的代码中,它给出了未为“x”指定的寄存器名称。如果有人知道它到底在说什么,以及与其他存储类相比,寄存器变量的范围是什么,请指导我? 谢谢。!!!

 below is the output of terminal using gcc compiler in Ubuntu.

shilpi@shilpi:~/storageclass$ gcc register.c
register.c:9:15: error: register name not specified for ‘x’
register int x=10;
^
shilpi@shilpi:~/storageclass$ gcc register.c
register.c:9:15: error: register name not specified for ‘x’
register int x=10;
^



//int x = 10 ;
int z;
int main( )
{
extern int y ;
// register int x ;
printf ( "The value of x is %d \n", x ) ;
printf ( "The value of y is %d",y ) ;
z=add();
printf("\nvalue of sum is : %d", z);
return 0;

}
int y = 200 ;

int add()
{
int sum;
sum=x+y;
//printf("value of sum is : %d", sum);
}

最佳答案

在标准 C 中,register 只允许在函数内部使用,然后是从其声明到该声明发生的 block 末尾的可见范围这样的变量。

因为这样的 register 与您平台的硬件寄存器没有太大关系,所以它用词不当。它的目的是一个优化提示:你不能取这样一个变量的地址。

Gcc 有一个扩展,也允许将这样的 register 变量关联到真实的硬件寄存器。该扩展名也可以在文件范围内使用,这是您看到的错误消息。

关于c - 寄存器存储类的范围是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26039118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com