Difference between File Scope and Global Scope(文件作用域和全局作用域的区别)
问题描述
我是一名学生,我对 C 和 C++ 中的全局和文件范围变量感到困惑.两种观点有什么不同吗?如果是,请详细说明.
I am a student and I am confused about global and file scope variables in C and C++. Is there any difference in both perspectives? If yes, please explain in detail.
推荐答案
具有文件作用域的变量可以被单个文件中的任何函数或块访问.要声明文件范围的变量,只需在块外声明一个变量(与全局变量相同),但使用 static 关键字.
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 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.
这篇关于文件作用域和全局作用域的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:文件作用域和全局作用域的区别


基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01