gpt4 book ai didi

c - 为什么使用 'extern' 创建变量是声明而不是定义?

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:08 25 4
gpt4 key购买 nike

我在阅读时遇到了以下问题......只是无法理解这背后的逻辑。

auto int c;
static int c;
register int c;
extern int c;

假设前三个是定义,最后一个是声明..怎么来的?

最佳答案

最后一个带extern的没有为c定义存储。它仅表示 c 存在于某处,链接器应该能够将其解析为在别处定义的某个全局 c

如果您编译并链接了单个 .c 文件并尝试使用最后一个 c,您将遇到链接器错误。对于前 3 个 c,您不会因为它们在当前编译单元中有实质内容(它们已被定义)。

如果您想了解更多关于 extern 和声明与定义的信息,请点击此处 good article关于这个话题。引用那篇文章:

Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them

关于c - 为什么使用 'extern' 创建变量是声明而不是定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11507423/

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