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
- 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
May be interested
- Use variables in Shella variable is a string of characters from which we assign a value. the assigned value can be a number, text, file name, device or any other type of data.
- How to add Python to the Windows PATH variableto help you through the hassle of adding python to your windows path after installation, look at the options and a few related steps.
- What is Windows PATH?if you are trying to run something that is not part of windows, you will need to add it to the path variable. that tells the system where to look for executable files when you request them.
- Storage class in C programminga storage class defines the scope (visibility) and the lifetime of a variable or / and functions in the c program. the preceding type specifications can be changed. here are the storage classes that can be used in the c program.
- How to set and list environment variables in Linuxif you want to do something that requires using the command line, you will still need to know about environment variables. this may seem like a complicated term, but actually environment variables are easy to understand.
- Functions in Pythonwhat is python function? how is the syntax, components, and function types in python? how to create functions in python? these questions will be answered in the python lesson below.
- Data type in C programmingin the c programming language, data types refer to the system extension used for variable declarations of different types. the type of variable determines the amount of memory used to store that variable and how the bits are stored when released
- Class member functions in C ++a member function of a class is a function that has its definition or prototype inside the class definition like any other variable. it works on any object of the class that is a member, and has access to all members of a class for that object.
- How to Code an Alert with a Variable Using Javascriptalerts with variables are among the most useful things in javascript coding. you can use them to refer to someone by their name, to make a mini madlibs game, or even for showing quiz results. this article will show you how to make a...
- How to Use Excel VBA Variable Data Typesyou will learn to create the different types of excel vba (visual basic for applications) variable data types. vba makes life easy for programmers because it can handle all the details involved in dealing with data automatically. for...