“文件范围"和“文件范围"之间有什么区别?和“程序范围" [英] What is the difference between "File scope" and "program scope"
问题描述
据说全局声明的变量具有程序范围
用static关键字全局声明的变量被认为具有文件作用域.
A variable declared globally is said to having program scope
A variable declared globally with static keyword is said to have file scope.
例如:
int x = 0; // **program scope**
static int y = 0; // **file scope**
static float z = 0.0; // **file scope**
int main()
{
int i; /* block scope */
/* .
.
.
*/
return 0;
}
这两者有什么区别?
推荐答案
在C99中,没有什么叫做程序作用域".在您的示例中,变量x
具有一个文件范围,该文件范围终止于转换单元的末尾.声明为static
的变量y
和z
也具有文件范围,但具有内部链接.
In C99, there's nothing called "program scope". In your example variable x
has a file scope which terminates at the end of translation unit. Variables y
and z
which are declared static
also have the file scope but with internal linkage.
C99(6.2.2/3)如果声明了对象的文件作用域标识符 或包含静态存储类说明符的函数, 标识符具有内部链接
C99 (6.2.2/3) If the declaration of a file scope identifier for an object or a function contains the storage class specifier static, the identifier has internal linkage
此外,变量x
具有外部链接,这意味着名称x
可以被其他翻译单位或整个程序访问.
Also, the variable x
has an external linkage which means the name x
can is accessible to other translation units or throughout the program.
C99(6.2.2/5)如果对象的标识符声明具有 文件范围,没有存储类说明符,它的链接是外部的.
C99 (6.2.2/5) If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.
这篇关于“文件范围"和“文件范围"之间有什么区别?和“程序范围"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!