Half an Hour to Variables

I wrote this article yesterday when I was getting bored and was feeling too
lazy, all thanks to the hot weather here. I planned of meeting my friends but
at last I insisted on staying at home when I wrote this. Regarding the title,
I thought that if it took me an hour to write it then you would have to hardly
give Half an Hour to Variables.

Now coming to the serious stuff…


What is Variable


During program run we need to manipulate data and the data to be manipulated
are stored in memory locations known as variable. In the system memory, data
are stored in memory locations such as 0x00243 which are not easy to remember
if we have quite a lot of them. Variables are memory locations having a name
so that different memory locations can be distinguished easily.


Declaration and Initialization of Variables


The declaration of variable generally takes the following form:

type varname;

Here type is any valid data type and varname is the variable name.

Ex.

int age;

float temp;

Static initialization: Initialization means giving initial
value to a variable.

Ex. int var=100;

Here 100 is the static number with which the variable is initialized (given
initial value). This is type of initialization is known as static initialization
because the initial value given to the variable is known at the time of programming.

Dynamic initialization: Have a look at the following line
of code:

float avg=sum/count;

H ere avg is initialized with a value that is not known at the time of programming
and it may have different values during different program runs.(since sum and
count on which the value of avg depends, may have different values under different
circumstances)

Access Modifiers


Access modifiers change the way we access or modify a particular variable.

The two types of access modifiers are discussed below:

const: By declaring any variable as const, we are telling
the C++ compiler that the value of the variable will remain unchanged during
program run. If we try to modify a variable declared as const, the compiler
will report an error and the program won’t compile.

   //program to illustrate const Access Modifier
//this program would RUN
#include<iostream.h>
void main(void)
{
const float pi=3.14;//declration and initialization of variable as const

int rad;
float area;
cout<<"Enter radius of circle:";
cin>>rad;
area=(rad*rad)*pi;//here the variable is only being accessed an NOT modified

cout<<endl<<"Area of the circle:"<<area;

}

Let us have a look at another program:


   //program to illustrate const Access Modifier
//this program would NOT RUN
#include<iostream.h>
void main(void)
{
const float pi;//declaration of variable as const
pi=3.14;//variable is being modified, NOT ALLOWED
int rad;
float area;
cout<<"Enter radius of circle:";
cin>>rad;
area=(rad*rad)*pi;//here the variable is only being accessed an NOT modified

cout<<endl<<"Area of the circle:"<<area;

}

volatile: By declaring any variable as volatile, we are telling
the compiler that the value of the variable can change by ways other than explicitly
defined by the program. In other words the value of the variable can be changed
by programs or routines outside the program (say, by the Operating System).

For example we can pass the variable declared as volatile to the operating system
clock routine to automatically keep track of the time changes.

It is necessary to declare those types of variables whose value can change outside
the program as volatile. C++ treats variables in such a way that if we don’t
and since the program is not modifying the variable’s value, the program
may output unexpected results.


Related Articles:


Check out this stream