A variable is a data storage location in memory that has been named. Variables can have data types of int, float, boolean, char, or double.
Variables are declared in C++ by giving the data type first, then the variable name, optionally followed by an initial value, and finally a semicolon.
This can be done in two ways:
A scope is a program region, and there are generally three places where variables can be declared:
Following are the Types of Variables in C++
Local variables are declared and initialized at the beginning of a function or block, and memory is allocated inside its execution scope. Only statements within that function can access the local variable. Such variables are removed when the control exits the function.
Instance variables are non-static variables defined in a class outside of constructors, methods, and other blocks. Their memory is allocated when an object of the class in which they are declared is created, and it is destroyed when the object is destroyed. Their initialization is optional when declaring; by default, they will contain garbage values.
These are identical to instance variables but apply to all objects in the class. Each class has only one copy of static variables, which are specified using the static keyword. Their memory is allotted at the start of the program and destroyed when it is terminated.
All local variables are automatic by default. They are also referred to as automatic variables.
External or global variables are declared with the extern keyword. You can declare an external variable several times, but the value can only be assigned once. Their default value is zero or null.