Cplusplus 简明教程
Variable Scope in C++
作用域是程序的一个区域,广义上讲,有三个地方可以声明变量 −
A scope is a region of the program and broadly speaking there are three places, where variables can be declared −
-
Inside a function or a block which is called local variables,
-
In the definition of function parameters which is called formal parameters.
-
Outside of all functions which is called global variables.
C++ 变量作用域主要分为两部分 −
C++ variables scopes are categorized mainly two parts −
-
Local Variables
-
Global Variables
我们将在后续章节中学习什么是函数及其参数。在这里,让我们解释一下什么是局部变量和全局变量。
We will learn what is a function and it’s parameter in subsequent chapters. Here let us explain what are local and global variables.
Local Variables
在函数或块中声明的 Variables 是局部变量。它们只能由该函数内部或代码块内部的语句使用。局部变量不会被其自身外部的函数所知晓。
Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own.
Global Variables
全局变量在所有函数的外部定义,通常在程序的顶部。全局变量将在您程序的整个生命周期中保留其值。
Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program.
任何函数都可以访问全局变量。也就是说,全局变量在声明后可以在整个程序中使用。
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.
Local and Global Variables with Same Names
一个程序可以为局部变量和全局变量使用相同的名称,但函数内的局部变量的值将优先。
A program can have same name for local and global variables but value of local variable inside a function will take preference.
Accessing Global Variable
通过在该变量的名称前使用 SRO (作用域解析运算符) :: ,当存在具有相同名称的局部变量时可以访问全局变量。
You can access a global variable when there is a local variable with the same name by using the SRO (Scope Resolution Operator) :: before the name of that variable.
Example
在下例中,我们有同名的全局变量和局部变量,并且访问并打印全局变量的值 −
In the following example, we have global and local variables with the same name, and accessing and printing the value of the global variable −
#include <iostream>
using namespace std;
// Global variable declaration:
int g = 20;
int main() {
// Local variable declaration:
int g = 10;
cout << "Value of g (Local variable): " << g;
cout << endl;
cout << "Value of g (Global variable): " << ::g;
return 0;
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Value of g (Local variable): 10
Value of g (Global variable): 20
Initializing Local and Global Variables
当定义局部变量时,它不会由系统初始化,您必须自己初始化它。当您按照以下方式定义全局变量时,它们会自动由系统初始化 −
When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them as follows −
Data Type |
Initializer |
int |
0 |
char |
'\0' |
float |
0 |
double |
0 |
pointer |
NULL |
适当初始化变量是一种良好的编程实践,否则,有时程序可能会产生意外的结果。
It is a good programming practice to initialize variables properly, otherwise sometimes program would produce unexpected result.