Variables
Variables
are user-defined names assigned to specific memory locations in which the
values are stored. Variables are also identifiers; and hence, the rules for
naming the identifiers should be followed while naming a variable. These are called
as symbolic variables because these are named locations.
There
are two values associated with a symbolic variable; they are R-value and L-value.
•
R-value is data stored in a memory location
•
L-value is the memory address in which the R-value is stored.
Remember
that, the memory addresses are in the form of Hexadecimal values
Every
variable should be declared before they are actually used in a program.
Declaration is a process to instruct the compiler to allocate memory as per the
type that is specified along with the variable name. For example, if you
declare a variable as int type, in Dev C++, the compiler allocates 4 bytes of
memory. Thus, every variable should be declared along with the type of value to
be stored.
More
than one variable of the same type can be declared as a single statement using
a comma separating the individual variables.
<data type> <var1>,
<var2>, <var3> …… <var_n>;
Example:
int num1, num2, sum;
In
the above statement, there are three variables declared as int type. Which
means, in num1, num2 and sum, you can store only integer
values.
For
the above declaration, the C++ compiler allocates 4 bytes of memory (i.e. 4
memory boxes) for each variable.
If
you declare a variable without any initial value, the memory space allocated to
that variable will be occupied with some unknown value. These unknown values
are called as “Junk” or “Garbage” values.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum;
cout << num1 << endl;
cout << num2 << endl;
cout << num1 + num2;
}
In the above program, some unknown values will be occupied
in memory that is allocated for the variables num1 and num2; and the
statement cout << num1 + num2;
will display the sum of those unknown junk values.
Assigning
an initial value to a variable during its declaration is called as
“Initialization”.
int num = 100;
float pi = 3.14;
double price = 231.45;
Here,
the variables num, pi, and price have been initialized during the declaration.
These initial values can be later changed during the program execution.
#include <iostream>
using namespace std;
int main()
{
float pi = 3.14, radius, height, CSA;
cout <<
"\n Curved Surface Area of a cylinder";
cout <<
"\n Enter Radius (in cm): ";
cin >> radius;
cout <<
"\n Enter Height (in cm): ";
cin >> height;
CSA =
(2*pi*radius)*height;
system("cls");
cout <<
"\n Radius: " << radius <<"cm";
cout <<
"\n Height: " << height << "cm";
cout <<
"\n Curved Surface Area of a Cylinder is " << CSA
<<" sq. cm.";
}
Curved Surface Area of a cylinder
Enter Radius (in cm): 7
Enter Height (in cm): 20
Radius: 7cm
Height: 20cm
Curved Surface Area of a Cylinder is 879.2 sq. cm.
Variables that are of the same type can be initialized in a
single statement.
Example:
int x1 = -1, x2 = 1, x3, n;
A
variable can be initialized during the execution of a program. It is known as
“Dynamic initialization”. For example,
int num1, num2, sum;
sum = num1 + num2;
The
above two statements can be combined into a single one as follows:
int sum = num1+num2;
This
initializes sum using the known values of num1 and num2 during the execution.
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout <<
"\n Enter number 1: ";
cin >> num1;
cout <<
"\n Enter number 2: ";
cin >> num2;
int sum = num1 + num2; // Dynamic
initialization
cout << "\n Average: "
<< sum /2;
}
Enter number 1: 78
Enter number 2: 65
Average: 71
In
the above program, after getting the values of num1 and num2, sum is declared
and initialized with the addition of those two variables. After that, it is
divided by 2.
#include <iostream>
using namespace std;
int main()
{
int radius;
float pi = 3.14;
cout <<
"\n Enter Radius (in cm): ";
cin >> radius;
float perimeter = (pi+2)*radius; //
dynamic initialization
float area = (pi*radius*radius)/2; //
dynamic initialization
cout <<
"\n Perimeter of the semicircle is " << perimeter <<
" cm";
cout <<
"\n Area of the semicircle is " << area << "
sq.cm";
}
Enter Radius (in cm): 14
Perimeter of the semicircle is 71.96 cm
Area of the semicircle is 307.72 sq.cm
const
is the keyword used to declare a constant. You already learnt about constant in
the previous chapter. const keyword modifies / restricts the accessibility of a
variable. So, it is known as Access modifier.
int num = 100;
The
above declares a variable num with an initial value 100. However, the value of
num can be changed during the execution. If you modify the above definition as const int num
100; the variable num becomes a constant and its value will remain as 100 throughout the program, and it can never be changed during the execution.
#include <iostream>
using namespace std;
int main()
{
const int num=100;
cout <<
"\n Value of num is = " << num;
num = num + 1; // Trying to increment the
constant
cout <<
"\n Value of num after increment " << num;
}
For
the above code, an error message will be displayed as “Cannot modify the const object”
in Turbo compiler and “assignment of
read only memory num” in Dev C++.
A
reference provides an alias for a previously defined variable. Declaration of a
reference consists of base type and an & (ampersand) symbol; reference
variable name is assigned the value of a previously declared variable.
<type> <&
reference_variable> = <original_variable>
#include <iostream>
using namespace std;
int main()
{
int num;
int &temp = num; //declaration of a
reference variable temp
num = 100;
cout <<
"\n The value of num = " << num;
cout <<
"\n The value of temp = " << temp;
}
The value of num = 100
The value of temp =
100
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.