文件作用域和全局作用域:C & C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21898770/
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
File Scope and Global Scope: C & C++
提问by Iqbal Haider
I am a student and I am confused about global and file scope variables in C and C++. Is there is any difference in both perspectives? If yes, please explain in detail.
我是一名学生,我对 C 和 C++ 中的全局和文件范围变量感到困惑。两种观点有什么不同吗?如果是,请详细说明。
回答by Hitesh Vaghani
A variable with file scope can be accessed by any function or block within a single file. To declare a file scoped variable, simply declare a variable outside of a block (same as a global variable) but use the static keyword.
具有文件作用域的变量可以被单个文件中的任何函数或块访问。要声明文件范围的变量,只需在块外声明一个变量(与全局变量相同),但使用 static 关键字。
static int nValue; // file scoped variable
float fValue; // global variable
int main()
{
double dValue; // local variable
}
File scoped variables act exactly like global variables, except their use is restricted to the file in which they are declared.
文件范围变量的作用与全局变量完全一样,只是它们的使用仅限于声明它们的文件。
回答by Rufus
It is perhaps clearer to illustrate file (translation unit)-scope vs global scope when there are actually multiple translation units...
当实际上有多个翻译单元时,说明文件(翻译单元)范围与全局范围可能更清楚......
Take 2 files (each being it's own translation unit, since they don't include each other)
取 2 个文件(每个文件都是它自己的翻译单元,因为它们不包含彼此)
other.cpp
其他.cpp
float global_var = 1.0f;
static float static_var = 2.0f;
main.cpp
主程序
#include <cstdio>
extern float global_var;
//extern float static_var; // compilation error - undefined reference to 'static_var'
int main(int argc, char** argv)
{
printf("%f\n", global_var);
}
Hence the difference is clear.
因此区别很明显。
回答by Raging Bull
A name has file scope
if the identifier's declaration appears outside of any block. A name with file scope and internal linkage is visible from the point where it is declared to the end of the translation unit.
file scope
如果标识符的声明出现在任何块之外,则名称具有。具有文件作用域和内部链接的名称从它的声明点到翻译单元的末尾都是可见的。
Global scope
or global namespace scope
is the outermost namespace scope of a program, in which objects, functions, types and templates can be defined. A name has global namespace scope if the identifier's declaration appears outside of all blocks, namespaces, and classes.
Global scope
或者global namespace scope
是程序的最外层命名空间范围,可以在其中定义对象、函数、类型和模板。如果标识符的声明出现在所有块、名称空间和类之外,则名称具有全局名称空间范围。
Example:
例子:
static int nValue; // file scoped variable
float fValue; // global variable
int main()
{
double dValue; // local variable
}
Read more here.
在这里阅读更多。
回答by user3148898
File scope: Any name declared outside all blocks or classes has file scope. It is accessible anywhere in the translation unit after its declaration. Names with file scope that do not declare static objects are often called global names.
文件范围:在所有块或类之外声明的任何名称都具有文件范围。它在声明后可以在翻译单元的任何地方访问。文件范围内不声明静态对象的名称通常称为全局名称。
In C++, file scope is also known as namespace scope.
在 C++ 中,文件作用域也称为命名空间作用域。
回答by user596031
Read this carefully now.
现在仔细阅读。
You use those #include<'...'.h>statements at the top of your program/code. What you actually are doing there is telling the computer to refer to the functions prewrittenin those *h*eader files.That is, those functions have file scope.You donot write the code of printf scanf and functions like these cauz they are somewhere in header files.
您在程序/代码的顶部使用那些#include<'...'.h>语句。你实际上在做什么是告诉计算机参考那些*h*eader 文件中预先编写的函数。也就是说,这些函数具有文件范围。你不要编写 printf scanf 和这些函数的代码,因为它们在某处头文件。
Variables declared outside a function have "file scope," meaning they are visible within the file. Variables declared with file scope are visible between their declaration and the end of the compilation unit (.c file) and they implicitly have external linkage and are thus visible to not only the .c file or compilation unit containing their declarations but also to every other compilation unit that is linked to form the complete program.
在函数外部声明的变量具有“文件范围”,这意味着它们在文件中可见。用文件作用域声明的变量在它们的声明和编译单元(.c 文件)的结尾之间是可见的,并且它们隐含地具有外部链接,因此不仅对包含它们的声明的 .c 文件或编译单元可见,而且对所有其他文件或编译单元可见链接起来形成完整程序的编译单元。
Global variables can, as the name suggests, be considered to be accessible globally(everywhere)
全局变量,顾名思义,可以认为是全局可访问的(随处可见)