C++ Variable Scope
Generally, there are three places where variables can be defined:
-
Variables declared inside a function or a block of code are calledlocal variables.
-
Variables declared in the definition of function parameters are calledformal parameters.
-
Variables declared outside all functions are calledGlobal Variables.
A scope is a region of the program; a variable's scope can be classified as follows:
-
Local scope: Variables declared inside a function have local scope and can only be accessed within that function. Local variables are created each time the function is called and destroyed after the function finishes executing.
-
Global scope: Variables declared outside all functions and blocks have global scope and can be accessed by any function in the program. Global variables are created when the program starts and destroyed when the program ends.
-
Block scope: Variables declared inside a block have block scope and can only be accessed within that block. Block-scope variables are created each time the block is executed and destroyed after the block finishes executing.
-
Class scope: Variables declared inside a class have class scope and can be accessed by all member functions of the class. The lifetime of a class-scope variable is the same as that of the class.
**Note:** If a variable declared in an inner scope has the same name as a variable in an outer scope, the variable in the inner scope will shadow the one in the outer scope.
local variables
Variables declared inside a function or a block of code are called local variables. They can only be used by statements inside the function or block. The example below uses local variables:
Example
#include
<
iostream
using
namespace
std;
int
main
(
)
{
// local variable declaration
int
a
,
b
;
int
c
;
// actual initialization
a
=
10
;
b
=
20
;
c
=
a
b
;
cout
<<
c
;
return
0
;
}
Global Variables
Variables defined outside all functions (usually at the top of the program) are called global variables. The value of a global variable remains valid throughout the entire lifetime of the program.
Global variables can be accessed by any function. That is, once declared, a global variable is available throughout the entire program. The example below uses both global and local variables:
Example
#include
<
iostream
using
namespace
std;
// global variable declaration
int
g
;
int
main
(
)
{
// local variable declaration
int
a
,
b
;
// actual initialization
a
=
10
;
b
=
20
;
g
=
a
b
;
cout
<<
g
;
return
0
;
}
In a program, local and global variables can have the same name, but inside a function the value of the local variable shadows that of the global variable. Here is an example:
Example
#include
<
iostream
using
namespace
std;
// global variable declaration
int
g
=
20
;
int
main
(
)
{
// local variable declaration
int
g
=
10
;
cout
<<
g
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
10
Initializing Local and Global Variables
When a local variable is defined, the system does not initialize it; you must initialize it yourself. When a global variable is defined, the system automatically initializes it to the following values:
| Data Type | Default Initialization Values |
|---|---|
| int | 0 |
| char | '\0' |
| float | 0 |
| double | 0 |
| pointer | NULL |
Properly initializing variables is a good programming practice; otherwise the program may sometimes produce unexpected results.
Block scope refers to variables declared inside a block of code:
Example
#include
int
main
(
)
{
int
a
=
10
;
{
int
a
=
20
;
// block-scope variable
std
::
cout
<<
"Block variable: "
<<
a
<<
std
::
endl
;
}
std
::
cout
<<
"External variable: "
<<
a
<<
std
::
endl
;
return
0
;
}
In the example above, a variable named a is declared in the inner block; it shares the same name as the variable a in the outer scope. The variable a in the inner scope shadows the one in the outer scope: accessing a in the inner scope outputs 20, while accessing a in the outer scope outputs 10.
When the code above is compiled and executed, it produces the following result:
块变量: 20
外部变量: 10
Class scope
Class scope refers to variables declared inside a class:
Example
#include
class
MyClass
{
public
:
static
int
class_var
;
// class-scope variable
}
;
int
MyClass
::
class_var
=
30
;
int
main
(
)
{
std
::
cout
<<
"Class variable: "
<<
MyClass
::
class_var
<<
std
::
endl
;
return
0
;
}
In the example above, a class-scope variable named class_var is declared in the MyClass class. You can access this variable using the class name and the scope resolution operator ::. Accessing class_var in the main() function outputs 30.
类变量: 30