Variables are fundamental to any language. Values can be assigned
to variable, which can be changed in the course of program
execution. The value assigned to a variable is placed in the
memory allocated to that variable. Variables can be created using
the keywords int, char, and float.
We will now discuss the different types of variables used in C++.
Integer variables are used to store whole numbers. The following program
illustrate the usage of integer variable:
/*This program demonstrates
the usage of integer variables and
the usage of multiple line comment entries */
int iVar1; //Define an integer
int iVar2 = 10, iVar3; //Define the
integer variable iVar2 and assigns a value 10
// to it, and, also defines a
iVar1 = 5;
iVar3 = 6;
cout<<"The value of iVar1
cout<<"The value of iVar2
cout<<"The value of iVar3
int iVar2 = 10, iVar3;
in Program 1.2 define three integer variables:
iVar1, iVar2, and
iVar3. The keyword int specifes the data type of
endl is used to move the cursor to the next line.
The output of Program 1.2 is:
The value of iVar1 is 5
The value of iVar2 is 10
The value of iVar3 is 6
To display the value of a variable, the variable name should not be
enclosed within double quotes.
Character variables are used to store character constants like 'A', 'm',
and so on.
Character constants are enclosed within single quotes.
The following program illustrates the usage of character variables:
//This program demonstrates the usage of character variables
char cVar1; //Define a character
char cVar2 = 'A'; //Define a
character variable and assign a character constant
cVar = 'B';
cout<<"Value of cVar1 is "<< cVar1<<endl;
cout<<"Value of cVar2 is "<< cVar2<<endl;
The output of Program 1.3 is:
Value of cVar1 is B
Value of cVar2 is A
char cVar2 = 'A';
in Program 1.3 define memory locations for cVar1 and cVar2,
each of which is one byte in size. These variables can store only
one character each. To store more than one character, a string has
to be declared. For example,
allocates ten contiguous bytes to hold a string. The last byte is used to
store the string terminator (\0), also known as the NULL
The following program illustrates the usage of strings:
//This program demonstrate the usage of strings
char text; //Allocates 40 bytes
the variable text
cout<<"Enter a string: "<<endl;
cout<<"The string that you have
entered is: "<<text<<endl;
in the program caused the program to wait for the user to enter a value.
The usage of cin will be
discussed in more detail, later in the sesson.
In Program 1.4 if the user enters John, then the output will be:
The string that you have entered is: John
The float variables are used to store floating point numbers. Program 1.4
illustrate the usage of float variables.
//This program demonstrate the usage of float variables
float fVar1 = 20.50; //Define a float
variable and assigns a value to it
float fVar2; //Define a float
fVar2 = 50.20;
cout<<"The value of fVar1 is
"<<fVar1<<" and fVar2 is "<<fVar2<<endl;
float fVar1 = 20.50;
define two variables fVar1 and fVar2 of float type.
The output of Program 1.5 is:
The value of fVar1 is 20.5 and iVar2 is 50.2