A Detailed Study Of Variables In C++.
In this Full C++ Training Series this tutorial will explain variables in C++ which are the entities that we require to assign memory in order to store data. We know programming is nothing but the manipulation and processing of data.
When we are dealing with data, there has to be a way for the programmer to store this data in the computer’s memory.
As this data moves back and forth in the program, it is required that the programmer gives a name for that particular memory storage and also knows how much memory that data is going to take up and what type of data is going to be stored.
All these considerations make up a variable entity. Thus a variable has a data type, an identifier that allows us to identify the variable by the name and also the size of the data.
Although C++ has many other entities including constants, arrays, other data structures, and functions that deal with data, the variable is the simplest entity and hence we will discuss it first.
In this tutorial, we will discuss all the details pertaining to the variables right from declaring the variables to various scopes of variables in C++.
Table of Contents:
Variable Declaration/Definition
Variable declaration/definition consists of the data type of the variable followed by a variable name. Declaration tells the compiler how much memory to be reserved for this variable (depending on the data type).
The data type of the variable can be any valid C++ data type (we have already seen valid data types supported by C++). Name of the variable has to be a valid identifier which we discussed in one of our previous tutorials.
Following are some valid variable declarations.
int x, y, z; char mychar; float annual_salary;
The first declaration is also a definition and it tells the compiler to create three variables each of type int with names, x, y, and z, and reserve memory space for each of them.
Similarly, mychar is a variable of type character while annual_salary is a variable of type float.
Variable Initialization
Variables can also be initialized or assigned with initial values during the time of declaration. This initializer consists of a ‘=’ sign followed by a constant value/expression as follows:
type variable_name = value;
Note: When the variable declaration is without the initial values, the compiler initializes the variable with static storage to null and all the other variables to undefined.
Shown below are some examples of a variable with initializers.
float PI = 3.142; int payday = 1; char firstChar = ‘A’;
As the variables are evaluated at compile time, all variables must be declared before being used.
L-values And R-values For A Variable
L-values and R-values are relevant in the case of variable definitions.
We say an expression is “l-value” when it refers to a memory location. L-value may appear either on the left-hand or right-hand side of an expression.
An expression is “R-value” when the expression refers to a data value at a memory location. It cannot have a value assigned to it. Therefore, a R-value expression cannot appear on the left-hand side of the expression. It can appear only on the right-hand side.
So when we have the following statement:
Int I = 20;
This means that the variable ‘I’ is l-value while the value 20 is R-value.
If we have something like
5=10;
This is an invalid expression as the constant value can never appear on the left-hand side.
Size Of Variables
The size of a variable is nothing but the size of the data type of the variable. We have already seen that we can find the size of the data type using ‘sizeof’ operator in our data type tutorial.
Consider the following program where we have declared 3 variables of different types.
#include <iostream> using namespace std; int main() { int x=10; float salary; double average; cout<<"\nsize of x:"<<sizeof(x); cout<<"\nsize of salary:"<<sizeof(salary); cout<<"\nsize of average:"<<sizeof(average); }
Output:
size of x:4
size of salary:4
size of average:8
If we check the output of the above program, we find that the size of the three variables is nothing but the size of the data type of the variable.
Variable Scope
Variable scope is the region in which the variable remains active. The scope of a variable starts from the point it is declared. This is true for other entities as well.
In C++, we have three places where we declare the variable.
#1) Local Variables
Variables that are declared inside a particular block or function are called local variables. These variables are active and accessible only inside the block or function they are declared. They are not accessible outside the block or function.
#2) Global Variables
Global variables are global in nature i.e. they are declared outside the program. This means that they are accessible to all the functions including the main function. Global variables remain active throughout the program and go out of scope only when the program exits.
The following program shows the usage of global and local variables in C++.
#include <iostream> using namespace std; int globalVar = 20; int main() { int x=10; float salary = 10000.00; cout<<"\nValue of x:"<<x; cout<<"\nValue of salary:"<<salary; cout<<"\nValue of globalVar:"<<globalVar; }
Output:
Value of x:10
Value of salary:10000
Value of globalVar:20
A screenshot for the same is given below.
In the above program, we have two local variables x and salary, and another global variable globalVar. When we print the value of these variables, we see that the globalVar is accessible inside the main function as well.
Note: We can also have a variable with the same name in different scopes. Thus if we have a local variable and a global variable with the same name then the local variable will have a preference over the other variables with the same name.
Conclusion
This is all about the variables in C++.
In our upcoming tutorial, we will get to know about constants or literals that are used in C++.
=> Look For The Entire C++ Training Series Here