A variable is a name associated with a mutable value. For example, When i write int num=20; where the variable name is num which is associated with the value 20. int is a data type that indicates that this variable can hold an integer value. In the next tutorial, we'll look at data types. In this tutorial, we'll talk about variables.

Syntax of declaring a variable in C++

data_type variable1_name = value1, variable2_name = value2;

For example:

int num1=20, num2=100;

We can also write it like this:

int num1,num2;
num1=20;
num2=100;

Types of variables

Variables can be classified based on their data type. For example, in the example above we saw a variable of integer type. 

Following are the types of variables available in C++.

int: These type of of variables holds integer value.

char: holds character value like ‘a’, ‘S’, ‘H’, ‘r’, ‘t’ etc.

bool: holds boolean value true or false.

double: double-precision floating point value.

float: Single-precision floating point value.

Types of variables based on their scope

Before going further lets discuss what is scope first. When we discussed the Hello World Program, we have seen the curly braces in the program like this:

int main {

//Some code

}

Variables declared within these curly braces have a restricted scope within these curly braces. If you declare a variable in the main() function and try to use that variable outside the main() function, you will get a compile-time error.

Now that we have understood what is scope. Lets move on to the

Types of variables based on the scope.

1. Global variable
2. Local variable

Global Variable

Variables declared outside any function (including main) are called global variables. Global variables have program-wide scope and can be accessed anywhere in the program. Mainly accessible from anywhere in user-defined functions. 

Let's take an example to understand:

Global variable example

Here we have a global variable myVar, that is declared outside of main. We have accessed the variable twice in the main() function without any issues.

#include <iostream>
using namespace std;
// This is a global variable
char myVar = 'A';
int main()
{
   cout <<"Value of myVar: "<< myVar<<endl;
   myVar='B';
   cout <<"Value of myVar: "<< myVar;
   return 0;
}

Output:

Value of myVar: A
Value of myVar: B
 

Local variable

Local variables are declared within curly braces of a user-defined function, main function, loop, or control statement (if, if-else, etc.) and their scope is limited within these curly braces.

Local variable example

#include <iostream>
using namespace std;

char myFuncn() {
// This is a local variable
char myVar = 'A';
}
int main()
{
   cout <<"Value of myVar: "<< myVar<<endl;
   myVar='B';
   cout <<"Value of myVar: "<< myVar;
   return 0;
}

Output:
Compile time error, because we are trying to access the variable 
myVar outside of its scope. The scope of myVar is limited to the body of function myFuncn(), inside those braces.

Can local and global variable have same name in C++?

Lets see an example having same name global and local variable.

#include <iostream>
using namespace std;
// This is a global variable
char myVar = 'A';
char myFuncn() {
   // This is a local variable
   char myVar = 'B';
   return myVar;
}
int main()
{
   cout <<"Funcn call: "<< myFuncn()<<endl;
   cout <<"Value of myVar: "<< myVar<<endl;
   myVar='C';
   cout <<"Funcn call: "<< myFuncn()<<endl;
   cout <<"Value of myVar: "<< myVar<<endl;
   return 0;
}

Output:

Funcn call: B
Value of myVar: A
Funcn call: B
Value of myVar: C

As you can see that when I changed the value of myVar in the main function, it only changed the value of global variable myVar because local variable myVar scope is limited to the function myFuncn().