In the previous post, we had seen what are the various built-in data types in C++. We are now ready to use them to create variables in our programs. First let us see what variables actually are and how they work.

Definition :

Variables are symbolic names given to memory locations that store various values.

In other words, when we declare a variable and assign it any value, that value gets stored in memory at some location. And this value can change – it is not fixed.


  1. Variables need to be declared before they can be used in a program.
  2. This is done to tell the compiler about the name and type of the variable.
  3. For declaring a variable, we use this syntax: type name ;
  4. ‘type’ refers to the data type the variable will store.
  5. ‘name’ refers to the symbolic name used to denote the variable.
  6. The name can be any valid identifier.

As we have seen in the previous post, we have four basic built-in data types in C++: int, char, float, double. So we can have integer variables, floating-point variables, character variables and double variables.

For example:

  • int num1;
  • float num2;
  • char ch;
  • double num3;


  1. After declaring variables, we need to assign values to them. This is called variable initialization.
  2. Initialization Syntax:  variable_name = value;
  3. ‘=’ is the assignment operator. It assigns the value specified on its right side to the variable name on the left.
  4. value should be of the type we have specified in the variable declaration.

For example: The variables declared above  can be initialized as follows:

  • num1 = 5;
  • num2 = 12.9;
  • ch=’A’;

NOTE: We can also declare and initialize variables in a single statement. For eg:  int number=8;

Let us take up an example program to understand variables better. This program asks the user (student) to enter marks in three subjects. Then it calculates the total marks and percentage of marks.

using namespace std;
int main()
 int marks1, marks2, marks3;
 float total_marks,max_marks, percentage ;
 max_marks = 300; //100 for each subject
 cout << "Enter marks scored in English : " ;
 cin >> marks1;
 cout << "\n\nEnter marks scored in Science : " ;
 cin >> marks2;
 cout << "\n\nEnter marks scored in Maths : " ;
 cin >> marks3 ;
 total_marks = marks1 + marks2 + marks3 ;
 percentage = total_marks / max_marks * 100 ;
 cout << "\n\nTotal Marks : " << total_marks <<endl ;
 cout << "\n\nPercentage : " << percentage ;
 getch() ;
 return 0 ;

Here is a live preview of this program along with the output.

Declare and Initialize Variables in C++

Now let us see what is happening in this program.

int marks1, marks2, marks3;
 float total_marks,max_marks, percentage ;
 max_marks = 300; //100 for each subject

We have taken 3 integer variables to store marks of three subjects. And we have taken three float variables to store total marks, maximum marks, and percentage.

max_marks which store maximum marks of a student is initialized to value 300; although this variable is storing the value 300 which is an integer, it is automatically converted to float by the compiler.

total_marks = marks1 + marks2 + marks3 ;
 percentage = total_marks / max_marks * 100 ;

total_marks and percentage are then calculated.

Notice that both these variables are initialized during program execution, using values of other variables and not with constant values. Even this is possible with variables!

So this is all about variables in C++. Hope you have understood the topic. Do let us know your queries in comments below.


Founder & Senior Editor of and Editor at Sakshi is currently pursuing MCA. She is a foodie and has a unique love for learning computer programming languages. Follow her on Google + | Facebook | Twitter. Contact her at [email protected] for feedback and sending tips.

Tags: , , , , ,

1 Comment on this article. Feel free to join this conversation.

  1. John January 7, 2017 at 10:28 pm - Reply

    1. Want to write and run a C++ program that show how my system responds to uninitialised variable for different data types.
    2. Write and run a C++ program that print the first letter of my last name as a block letter of #
    3. Write and run a C++ program that calculate the area of a circle.

Leave A Response