Variable scope in C ++
A scope is an area of the program where the variable operates, and in general there can be three areas where the variable can be declared:
- Inside a function or block, it is called a local variable.
- In the definition of function parameters, it is called formal parameters.
- Outside of all functions, called global variables.
We will learn what functions and parameters of functions are in the next chapter. Below we will explain the concept of local variables and global variables.
Local variable in C ++
Variables declared inside a function or block are local variables. They can only be used by commands within that function or code block. Local variables are not known outside of that function (ie used only within that function or code block). Here is an example using local variables:
#include using namespace std; int main () { // phan khai bao bien cuc bo: int a, b; int c; // phan khoi tao bien a = 10; b = 20; c = a + b; cout << c; return 0; }
Global variables in C ++
The global variable (global) in C ++ is defined outside the functions, usually at the beginning of the program. Global variables keep their values throughout your program lifecycle.
A global variable can be accessed by any function. That is, a global variable is available for you to use throughout the program after declaring it. Here is an example to use global variables and internal variables in C ++:
#include using namespace std; // phan khai bao bien toan cuc: int q; int main () { // phan khai bao bien cuc bo: int t, m; // phan khoi tao bien q = 88; t = 12; m = q + t; cout << q; return 0; }
A program can have global variables and local variables with the same name, but in a function, the value of the local variable will take precedence. For example:
#include using namespace std; // phan khai bao bien toan cuc: int g = 20; int main () { // phan khai bao bien cuc bo: int g = 10; cout << g; return 0; }
When the above code is compiled and executed, it gives the following result:
10
Initialize local and global variables by system in C ++
When a local variable is defined, it is not initialized by the system, you must initialize it. Global variables are initialized automatically by the system when you define them, as follows:
Data type 0 char initial value '' float 0 double 0 pointer NULLInitializing the variable correctly is a good practice, otherwise, the program will sometimes produce unexpected results.
According to Tutorialspoint
Previous post: Variable type in C / C ++
Next article: Hang (Constant / Literal) in C / C ++
You should read it
- Scope rules in programming C
- Variable in JavaScript
- Global keywords in Python
- Global variables (global), local variables (local), nonlocal variables in Python
- Declare variables in SQL Server
- Variables are predefined in PHP
- Learn about custom variables in Google Analytics
- Variable in C programming
- Use variables in Shell
- How to set and list environment variables in Linux
- How to add an environment variable in Windows 10
- Variable type in C / C ++