Introducing Variables

Learn about the integer variable, the assignment operator, and the memory allocation.

In this lesson, we will learn about the concept of variables.

Table printing: Why use variables?

In the printing table example (from the previous lesson), we saw that we had to replace all the numbers explicitly with the number whose table we wanted to print. So if we wrote the code to print the table of 34 and then later we wanted to print the table of 67, we had to replace the number 34 with 67 throughout our code.

This is where variables come in and help make everything super easy for us. We can simply store the number in a variable and use that variable in our code. So when we want to print the table of a different number, we simply change the variable’s value. Let’s see how we can create, update and use variables in C++.


What is a variable?

A variable is a named location in the computer’s memory that stores some value. We can fetch or update the value by using the variable’s name.

For example, we create a variable named num in which 72 is stored, as shown in the image below.

Variables are of different types, such as integers, floating-point numbers (like real values), characters, boolean values (true/1 or false/0), etc. Each variable type has a different size and layout in the computer’s memory.

In this chapter, we will only use the integer type variable. We’ll learn about the other data types in the upcoming chapters.

Let’s first learn a little about the computer’s memory. The computer memory can be viewed as a row of sequentially numbered boxes. These numbers are known as memory addresses.

Variable declaration

When we declare a variable, we need to write its data type (i.e., which type of data the variable will hold). For example, we declare a variable num of type int that can hold integer values as follows:

int num;

We can also declare multiple variables by using the comma as follows:

int num1, num2, num3;

Variable initialization

When declaring the variable, we initialize the variable with a value as follows:

int num = 14;

If we don’t initialize a variable with any value, it is assigned a garbage value. Some compilers give a compile-time error if we use an uninitialized variable.

Assignment operator

The symbol that is used for assigning the value to a variable is called the assignment operator and is denoted by = in C++.

So to assign the value 34 to the num variable, we use the assignment operator: num = 34;. This means that the variable num is initialized with 34. This instruction is only applicable if the variable num is already declared before. Otherwise, num will be named as an unknown token leading to a syntax error.

The Declaration and initialization process can also be done in one line as follows:

int a = 5, b = 3, c = 0;

If we declare a variable (like int x;) without initializing it with any value, we can assign it a value later (like x = 1;) without writing its data type again.

For explanation’s sake, we have used the abbreviated variable names a, b, etc., so we can focus on the important concepts under discussion. However, a good programmer should always use descriptive and meaningful names to improve the code’s readability and maintainability.

Now let’s declare and initialize variables in the code editor below:

Press + to interact
#include <iostream>
using namespace std;
int main() {
// values are assigned to "a", "b", and "c" variables
int a = 6, b = 2, c = 6;
cout << "a = "<< a << ", b = " << b << ", c = " << c << endl;
// the result of the expression is stored in the "a" variable
a = b + 40;
cout << "Updated value after 'a = b + 40': a = " << a << endl;
// the initial value of "c" is
cout << "Value of c before: " << c << endl;
// assigning the "c" variable the value of variable "b":
c = b;
// now printing the value of "c" again
cout << "Value of c after c = b: c = " << c << endl;
return 0;
}

So in the code editor above in line 6, we assign values to the a, b, c variables.

  a = b + 40;

In line 10, we assign the result of the arithmetic expression a = b + 40 to the a variable.

In line 16, we assign the c variable with the value of b. The value of c, which was initially 6, now becomes 2.


Now that we’ve seen what variables are, let’s continue where we left off in the previous lesson.

Table printing (cont.)

Now that we understand what variables are and how they can be declared and initialized, let’s update our table printing program using variables.

Let’s see what happens when we replace the numbers with the variable Table in our table printing example (from the previous lesson):

Press + to interact
#include <iostream>
using namespace std;
//This program print the table of 73 up to 10th multiple
int main()
{
int Table = 73;
cout << "Table x 1 = " << Table*1 << endl;
cout << "Table x 2 = " << Table*2 << endl;
cout << "Table x 3 = " << Table*3 << endl;
cout << "Table x 4 = " << Table*4 << endl;
cout << "Table x 5 = " << Table*5 << endl;
cout << "Table x 6 = " << Table*6 << endl;
cout << "Table x 7 = " << Table*7 << endl;
cout << "Table x 8 = " << Table*8 << endl;
cout << "Table x 9 = " << Table*9 << endl;
cout << "Table x 10 = " << Table*10<< endl;
return 0;
}

When you click the “Run” button above, the code prints the table of 73 just fine except for the one logical error that we can see.

The code above is printing the word "Table" instead of the value "73" on the left-hand side of the entire table. This is because we have placed the "Table" inside the double quotes which prints the word "Table" as it is instead of its value.

So all we need to do is write Table outside of the double quotes to print its value like this:

Press + to interact
#include <iostream>
using namespace std;
//This program print the table of 73 up to 10th multiple
int main()
{
int Table = 73;
cout << Table << " x 1 = " << Table*1 << endl;
cout << Table << " x 2 = " << Table*2 << endl;
cout << Table << " x 3 = " << Table*3 << endl;
cout << Table << " x 4 = " << Table*4 << endl;
cout << Table << " x 5 = " << Table*5 << endl;
cout << Table << " x 6 = " << Table*6 << endl;
cout << Table << " x 7 = " << Table*7 << endl;
cout << Table << " x 8 = " << Table*8 << endl;
cout << Table << " x 9 = " << Table*9 << endl;
cout << Table << " x 10 = " << Table*10<< endl;
return 0;
}

Now, to print the table of any number instead of 73, we have a much more convenient way; we just need to change the value of Table by whichever number we would like to have, and it’s done!


Exercise

Replace the value of the Table variable with 83 in the code editor above and click “Run.” Do the same to print the table of 65.


So now we have seen how we can easily change the value of the Table variable to print any number’s table.

But wouldn’t it be better if the program could prompt the user about which table to print and then print the table according to the input provided?

This is where the cin instruction comes in. cin helps us avoid changing the code and allows us to print anything according to the user’s input.

We’ll see how in the next lesson.