c - 汇编中的静态和非静态变量

标签 c assembly

问题 1:以下说法正确吗:

In c file, a variable outside function which is not marked as static has scope from it's definition to end of the file.

问题2:如果问题1的答案为true,那么当c程序编译为汇编代码时,问题1中提到的变量存储在哪一段中?

@编辑:

I ask this question because I am a little confused about variable marked with static and variable not marked with static. What is the underling different in assembly for this 2 kind of variable, where are they stored? If they both stored at the data section, then how does c make sure the variable marked with static is only visible to the file that define it.

<小时/>

@答案:

According to answer & comment of @Vlad from Moscow, the answer to question 1 is true, the answer to question 2 is "data section", and the reason that staic variable is only visible to the file defined it is that static variable has internal linkage to make sure it is not visible outside the compilation unit where it is declared.

最佳答案

根据C标准(6.2.1标识符的范围)

4 Every other identifier has scope determined by the placement of its declaration (in a declarator or type specifier). If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit.

这是您第一个问题的答案。

对于第二个问题,变量存储在数据部分,并且具有静态存储持续时间。

编辑:使用存储类说明符 static 声明的变量具有内部链接,即它们在定义它们的编译单元之外不可见。

关于c - 汇编中的静态和非静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26316349/

相关文章:

c - libgcrypt-config --libs : no such file or directory

c - 无法使用 mongo-c 驱动程序将大型 json 文件导入 mongoDB

c - 不同结构的迭代器?

c - 错误 : expected identifier or '(' before numeric constant

c++ - 在 VS 代码中调试 NASM

windows - 为什么中断 0x2A 在 x64 中不起作用?

windows - 具有内联 asm 的 Windows x64 平台中的 libsoxr 未编译

c++ - ARM 程序集 - 访问参数与返回值?

c - fstcw 汇编操作数类型不匹配

c - 在C中查找字符数组的保留内存大小