Follow Us

LightBlog

Breaking

Wednesday, June 3, 2020

C++ Variable.

Variable:
➜A variable is the name of the memory location. 
➜It is used to store data.
➜ Its value can be changed and it can be reused many times.

Syntax of declaring a variable in C++
data_type variable1_name = value1;

For example:
int num1=202;

We can also write it like this:
int num1,num2;
num1=20;
num2=100;

                                           Types of variables:
Variables can be categorized based on their data type. For example, in the above example, we have seen integer types of variables. The following are the types of variables available in C++.

int: These type of variables holds an integer value.
char: holds character values like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.
bool: holds boolean value true or false.
double: double-precision floating-point value.
float: Single-precision floating-point value.

        ➤Let's move on to the types of variables based on the scope.
        1. Global variable.
        2. Local variable.

                                     Global variable   
A variable declared outside of any function (including main as well) is called a global variable.
➜ Global variables have their scope throughout the program, they can be accessed anywhere in the program, in the main, in the user-defined function, anywhere.
 Example
#include <iostream>
using namespace std;
// This is a global variable
char myVar = 'A';
int main()
{
   cout <<"Value of myVar: "<< myVar<<endl;
   myVar='Z';
   cout <<"Value of myVar: "<< myVar;
   return 0;
}
OUTPUT
Value of myVar: A
Value of myVar: Z
Hints
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.
                                  
                     Local variable
Local variables are declared inside the braces of any user-defined function, main function, loops, or any control statements(if, if-else, etc) and have their scope limited inside those 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='Z';
   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 global and local variables have the same name in C++?
#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='Z';
   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: Z

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().



No comments:

Post a Comment