Software Developer
A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
There are 5 types of variables in the C++ language which are as follows:
Local variables are declared inside the function. Local variables must be declared before they have been used in the program. Functions that are declared inside the function can change the value of variables. Functions outside cannot change the value of local variables.
Here is an example
int main()
{
int x = 2; //local variable
}
Global variables are declared outside the functions. Any function i.e. both local function and global function can change the value of global variables.
An example is given as follows,
int y = 10; //global variable
int main()
{
int x = 5; //local variable
}
These variables are declared with the word static.
An example is given as follows,
int main()
{
int x = 5; //local variable
static y = 2; //static variable
}
Automatic variables are declared with the auto keyword. All the variables that are declared inside the functions are default considered as automatic variables.
An example is given as follows,
int main()
{
int x = 20; //local variable (Automatic variable)
auto y = 12; //automatic variable
}
By using the extern keyword, external variables are declared.
extern z = 4; //external variable