Coupon Accepted Successfully!



  • We know that in C, all variables, must be declared before they are used in executable statements.
  • This is true with C++ as well.
  • Variables represent the storage location.
  • It stores the value of a particular data type inside a memory location.
  • For example to store the roll no of 10 students in a class. We have to assign a variable and store the data in a particular data type.
    • int a; // here the variable 'a' is declared with the data type integer. Hence it accepts integer type of data.
Variables are also called as symbolic variables because they are named location.

Declaration of Variables

  • All variables present in the program must be declared before it is used.

  • The declaration can also be done before the first use of the variable.

  • The general form is

    Data type variable list;

Data type - valid data such as int, float, char etc.
Variable list - variables separated by comma.


int a, b, c;

Initialization of Variables

  • All the example definitions of the previous values are simple definitions.
  • The variables are uninitialized and the variable's value is said to be defined.
  • C++ supports variable initialization at the time of variable definition
For example


int a = 10;

float b = 10.5;

int b = 10, c = 20;

Reference Variables

  • C++ introduces a new kind of variable known as the reference variable.
  • A reference variable provides an alias (alternative name) for a previously defined variable
  • For example, if we make the variable sum a reference to the variable total, then sum and total can be used interchangeably to represent that variable.
  • A reference variable is created as follows


Data type &reference name=variable-name

  • Example

float total =100;

float & sum=total;

Dynamic Initialization

  • One of the additional features of C++ is that it permits initialization of the variable at run time.
  • Initialization of variables during run time is called dynamic initialization of variables.
  • Example

int l;

cin >> l;

float avg = l /10;

Referencing Variable

  • A reference variable is a name that acts as an alternate name for a previously defined variable.
  • The general form is


data_type - data type of the already defined variable.
& - reference operator
new_name - any valid C++ variable name
  • Example

    int abc; // initializing the variable abc

    abc=20; // assign abc as 20;

    int & xyz = abc; // using reference operator a the value of abc

    is assigned to xyz

    cout <<xyz; // printing xyz as 20

  • Note: Variables if not initialized they are not empty. They contain some garbage values.

    Test Your Skills Now!
    Take a Quiz now
    Reviewer Name