gpt4 book ai didi

c - C中的声明与定义

转载 作者:行者123 更新时间:2023-12-04 11:28:27 25 4
gpt4 key购买 nike

考虑代码:

int main(void)
{
int a;
}

据我所知,int a; 是一个定义,因为它会导致存储空间被保留。引用 C 标准(N1570 委员会草案 — 2011 年 4 月 12 日):

6.7/5 Semantics A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

— for an object, causes storage to be reserved for that object;

...

问题来了:编译器可能会优化存储,因为我们没有使用变量。那么 int a; 是声明吗?如果我们在 main(void) 中执行 printf("%p", &a) 会怎样 - 当然现在编译器 必须 分配存储空间,那么声明/定义的概念是否取决于您以后是否使用标识符?

最佳答案

您从 6.7/5 中引用的文本实际上是要以与您所做的相反的方式解释:文本是说定义导致分配存储。

指定 int a; 是定义的文本在别处。

C 是根据抽象机器定义的。在抽象机中分配了存储空间。是否在您的 PC 上分配了任何内存是无关的。

关于c - C中的声明与定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33163028/

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