C++

C++ Variables – Tutorialspoint

A variable is a name that has a movable value attached to it. For instance, when I write int num=20, the data type int indicates that this variable can hold integer values and is connected with the value 20. The data types will be discussed in the upcoming tutorial. We’ll talk about variables in this tutorial.

Declaring a variable in C++ syntax

data_type variable1_name = value1, variable2_name = value2;

For example:

int num1=20, num2=100;

Another way to express it is as follows:

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

Types of variables

The data type of a variable might be used to categorize it. We saw integer type variables in the example above, for instance. The types of variables that are available in C++ are as follows.

int: These type of of variables holds integer value.

char: holds character value 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.

Types of variables based on their scope

Let’s first define what scope is before moving on. We saw the curly braces in the program like this when we reviewed the Hello World Program:

int main {

//Some code

}

Any variable defined inside these curly braces has scope that is only allowed to be used inside these curly braces; if you declare a variable inside the main() function and attempt to use it outside of the main() function, a compilation error will occur.

Now that we are aware of what scope is. Let’s move on to the various variable types according to the scope.

1. Global variable
2. Local variable

Global Variable

Global variables are variables that are declared outside of all functions, including main. Global variables have a scope that spans the entire program and can be accessed anywhere in it, including the main function and user-defined functions.

To better understand, let’s look at an example:

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='Z';
   cout <<"Value of myVar: "<< myVar;
   return 0;
}

Output:

Value of myVar: A
Value of myVar: Z

Local variable

Local variables are declared and have their scope limited inside the braces of any user-defined function, main function, loops, or control statements (if, if-else, etc.).

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 a C++ variable have the same name both globally and locally?

ets 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='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, because the scope of the local variable myVar is restricted to the function myFuncn, changing the value of myVar in the main function only affected the global variable myVar ().

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button