On the scope of C++ variables

  • 2020-10-07 18:47:40
  • OfStack

C++ variable scope

Scope is a region of a program. In general, there are three places to define variables:

Variables declared within a function or a block of code are called local variables. Variables declared in the definition of function parameters are called formal parameters. Variables declared outside all functions are called global variables.

We will learn what functions and parameters are in subsequent chapters. In this chapter we will first explain what local and global variables are.

A local variable

Variables declared within a function or a block of code are called local variables. They can only be used by statements within functions or blocks of code. The following example USES local variables:


#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;
}

The global variable

Variables defined outside of all functions (usually at the head of the program) are called global variables. The value of a global variable is valid throughout the life of the program.

Global variables can be accessed by any function. That is, once the global variable 1 is declared, it is available throughout the program. The following examples use global and local variables:


#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 within a function, the value of the local variable overrides the value of the global variable. Here is an 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 above code is compiled and executed, it produces the following results:

[

10

]

Initializes 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 the following values:

数据类型 初始化默认值
int 0
char '\0'
float 0
double 0
pointer NULL

It is a good practice to initialize variables correctly, otherwise sometimes the program may produce unexpected results.

In a program, local and global variables can have the same names.

But the local and global variables within the function are two independent variables that do not affect each other.

In the following code, the global variable defines 1 int g=99, and the local variable defines 1 int g=10. Since the scope of the two g is different, they are independent.


#include <iostream>
using namespace std;

//  Global variable declaration 
int g = 99;

//  Function declaration 
int func();

int main()
{
 //  Local variable declaration 
 int g = 10;
 //cout << g;
 int kk = func();
 cout << kk;
 return 0;
}

//  The function definitions 
int func()
{
 return g;
}

The above is to learn C++ variable scope details, more about C++ variable scope information please pay attention to other related articles on this site!


Related articles: