问题 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/