C++ Learning Resource Network!

 
 
 C++ Tutorials  >  Introduction to C++  >  Variables in C++
 

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

 Integer variables are used to store whole numbers. The following program illustrate the usage of integer variable:
 

     //Program 1.2
     /*This program demonstrates the usage of integer variables and
     the usage of multiple line comment entries */

     #include<iostream.h>
     void main()
     {
        int iVar1; //Define an integer variable iVar
        int iVar2 = 10, iVar3; //Define the integer variable iVar2 and assigns a value 10
        // to it, and, also defines a variable iVar3
        iVar1 = 5;
        iVar3 = 6;
        cout<<"The value of iVar1 is"<<iVar1<<endl;
        cout<<"The value of iVar2 is"<<iVar2<<endl;
        cout<<"The value of iVar3 is"<<iVar3<<endl;
     }


 The statemens,
     int iVar1;
     int iVar2 = 10, iVar3;


 in Program 1.2 define three integer variables: iVar1, iVar2, and iVar3. The keyword int specifes the data type of the variables.

 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

 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:
 

    //Program 1.3
    //This program demonstrates the usage of character variables

    #include<iostream.h>
    void main()
    {
        char cVar1; //Define a character variable cVar1
        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

 Defining Strings

 The statements,
     char cVar1;
     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,

 char cWord[10];

 allocates ten contiguous bytes to hold a string. The last byte is used to store the string terminator (\0), also known as the NULL character.

 The following program illustrates the usage of strings:

     //Program 1.4
     //This program demonstrate the usage of strings
     #include<iostream.h>
     void main()
     {
        char text[40]; //Allocates 40 bytes the variable text
        cout<<"Enter a string: "<<endl;
        cin>>text;
        cout<<"The string that you have entered is: "<<text<<endl; 
     }


 The statement:
     cin>>text;
 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

 Float Variables

 The float variables are used to store floating point numbers. Program 1.4 illustrate the usage of float variables.

     //Program 1.5
     //This program demonstrate the usage of float variables
     #incldue<iostream.h>
     void main()
     {
        float fVar1 = 20.50; //Define a float variable and assigns a value to it
        float fVar2; //Define a float variable
        fVar2 = 50.20;
        cout<<"The value of fVar1 is "<<fVar1<<" and fVar2 is "<<fVar2<<endl;
     }


 The statements
     float fVar1 = 20.50;
     float fVar2;

 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

 

 

| Bookmark This Site |   | Links |   | Search |   | Submit |   | Contact |

  Copyright 2008 - 3008  CppTutor.com All rights reserved.