gpt4 book ai didi

c语言,全局符号,局部符号详解

转载 作者:行者123 更新时间:2023-12-05 05:03:56 28 4
gpt4 key购买 nike

给定以下 C 代码

static int x = 0;

int what_is_this(void) {
static int y = 5;
x = x + y;
y = y + 1;
return y;
}

int main(void) {
int v = what_is_this();
printf("%d\n", v);
return v;
}

关于链接器,what_is_this 是全局符号吗?

x 是局部符号吗?

v 不是注册为符号吗?

最佳答案

根据此处:https://people.cs.pitt.edu/~xianeizhang/notes/Linking.html#symbol

  • global: global symbols that are defined by module m and that can be referenced by other modules. Global linker symbols correspond to nonstatic funcs and global vars that are defined without the static attribute.
  • external: global symbols that are referenced by module m but defined by some other module. Such symbols are called externals and correspond to funcs and vars that are defined in other modules.
  • local (static): local symbols that are defined and referenced exclusively by module m. Some local linker symbols correspond funcs and global vars that are defined with the static attribute. These symbols are visible anywhere within module m, but cannot be referenced by other modules.

所以简单来说全局符号是非静态的,非外部函数,非静态的,非外部变量,外部符号用extern声明,局部符号是静态的。

With respect to the linker, is what_is_this a global symbol?

不是外部的,不是静态的,所以是的

Is x a local symbol?

用静态声明,所以是

Is v not registered as a symbol?

链接器永远不会看到局部变量声明。(在函数中声明的静态变量之外。)

关于c语言,全局符号,局部符号详解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61416129/

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