C语言 static int a 和 int a 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4792048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between static int a and int a?
提问by Sandeep Pathak
Possible Duplicate:
Difference between 'global' and 'static global'
可能的重复:
“全局”和“静态全局”之间的区别
What is the difference between statements 1 and 2 :-
陈述 1 和 2 之间有什么区别:-
#include <stdio.h>
//In the global declaration area
static int a; // 1.
int b; // 2.
Thanks for help.
感谢帮助。
采纳答案by Victor Nicollet
A staticglobal variable is local to the translation unitit is defined in. So, if you define static int a;in two different translation units, this will create two independent variables. If you define a non-static global variable int b;in two translation units, you will experience a linker error (but you can use extern int b;in one of the two translation units to tell the linker that it should use the global variable from the other translation unit).
一个static全局变量是本地的翻译单元它在定义的。因此,如果你定义了static int a;两种不同的翻译单位,这将创建两个独立的变量。如果您int b;在两个翻译单元中定义了一个非静态全局变量,您将遇到链接器错误(但您可以extern int b;在两个翻译单元之一中使用来告诉链接器它应该使用来自另一个翻译单元的全局变量)。
回答by SiegeX
Both are variable definitions, however, the statickeyword applied to a variable in the "global declaration area" restricts that global variable to be seen only in the translation unitin which it is defined.
两者都是变量定义,但是,static应用于“全局声明区域”中的变量的关键字限制该全局变量只能在定义它的翻译单元中看到。
回答by Sachin Shanbhag
They are both in memory for the entire lifetime of the program. The variable that is declared static only has scope in the file in which it is declared where as the variable declared without static can be accessed from other files using an extern declaration.
它们在程序的整个生命周期都在内存中。声明为 static 的变量仅在声明它的文件中具有作用域,而未声明为 static 的变量可以使用 extern 声明从其他文件访问。
Original source - http://bytes.com/topic/c/answers/860211-global-variable-static-global-variable
原始来源 - http://bytes.com/topic/c/answers/860211-global-variable-static-global-variable
回答by Prasoon Saurav
static int a;
int b;
ahas internal linkage. bhas extern linkage.
a有内联。b有外部联系。
C99 6.2.2
C99 6.2.2
6.2.2 Linkages of identi?ers
1) An identi?er declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. There are three kinds of linkage: external, internal, and none.
2) In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identi?er with external linkage denotes the same object or function. Within one translation unit, each declaration of an identi?er with internal linkage denotes the same object or function. Each declaration of an identi?er with no linkage denotes a unique entity.
3) If the declaration of a ?le scope identi?er for an object or a function contains the storage- class speci?er static, the identi?er has internal linkage.
6.2.2 标识符的链接
1) 在不同范围或同一范围内多次声明的标识符可以通过称为链接的过程来引用相同的对象或函数。链接分为三种:外部链接、内部链接和无链接。
2) 在构成整个程序的一组翻译单元和库中,每个带有外部链接的特定标识符的声明都表示相同的对象或函数。在一个翻译单元中,带有内部链接的标识符的每个声明都表示相同的对象或函数。没有链接的标识符的每个声明都表示一个唯一的实体。
3) 如果对象或函数的文件范围标识符声明包含存储类说明符静态,则标识符具有内部链接。
回答by Marlon
static int ais only accessible within that file. int bcan be accessed with extern int bfrom a different file.
static int a只能在该文件中访问。int b可以extern int b从不同的文件访问。
回答by Aurum Aquila
A static variable's life extends across the lifetime of the program. However, scope rules still apply.
静态变量的生命周期贯穿整个程序的生命周期。但是,范围规则仍然适用。
If you define your static variable outside of a method (normally at the beginning of the class) your variable will be available from anywhere within that class.
如果您在方法之外(通常在类的开头)定义静态变量,则您的变量将在该类中的任何地方可用。
You can't change the value of these objects. They're normally used for storing things like API keys.
您无法更改这些对象的值。它们通常用于存储 API 密钥之类的东西。

