静态 vs 全局 [英] Static vs global
本文介绍了静态 vs 全局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个像下面这样的 C 文件,i
和 j
有什么区别?
#include #include 静态 int i;国际 j;int主(){//一些实现}
解决方案
i
具有内部链接,因此您不能在其他源文件中使用名称 i
(严格翻译单元)引用同一个对象.
j
有外部链接,所以如果你在另一个翻译单元中声明它extern
,你可以使用 j
来引用这个对象.>
If I have a C file like below, what is the difference between i
and j
?
#include <stdio.h>
#include <stdlib.h>
static int i;
int j;
int main ()
{
//Some implementation
}
解决方案
i
has internal linkage so you can't use the name i
in other source files (strictly translation units) to refer to the same object.
j
has external linkage so you can use j
to refer to this object if you declare it extern
in another translation unit.
这篇关于静态 vs 全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文