Lesson 2: Variables

Variables are created in computer programs so that they can hold certain values. If we want to write a program to add two numbers, then we would need to have two variables to hold the numbers so that they can add up together. We shall discuss some of the types of variables are are available for you to use in a program.

Types of variables

The most common variable types that we shall use in our programs are:

  • int: integers only hold whole numbers. They have a small range.
  • longint: long integer are integers that have larger range.
  • double: these are numbers that have decimal places. Double type is able to store more digits.
  • char: characters are single letters, digits or symbols that are uniquely represented by an integer in the ASCII table.
  • string: strings stores anything within a pair of double quotes. You can use strings to store a sentence, a paragraph or a word. If you store a number as a string(within double quotes), this “number” will not be treated as a number. We shall look into the details below.

Declaring variables

A computer does not know anything unless the programmer tells it what to do. As a result, your computer program would not understand any variables that you create unless you explicitly instruct your program that you are creating those variables. We call this “variable declaration”.

We have studied some keywords from the previous class. std::cout is an example. The reason why your program understands what std::cout is is because you have the header line #include <iostream>. When you include that statement, you are giving your program a ready made list of important keywords that are used in input/output operations. Hence, your program understands what std::cout is.

//Program 2.1
#include <iostream>
int main {
  std::cout << "Hello world!";
  return 0;

You should be able to run this program successfully. Now, edit the program by removing the #include statement.

int main {
  std::cout << "Hello world!";
  return 0;

Your compiler will prompt an error message.

We shall now declare some variables.

//Program 2.2
#include <iostream>
int main {
  int x; // declare variable x as an int
  int y; // declare variable y as an int
  x = 1; // assigning value 1 to x
  y = 2; // assigning value 2 to y
  std::cout << x + y << std::endl;

  return 0;

You should get a number 3 in the console output. When you declare a variable, you are telling the computer to allocate a space in the memory to store the variable.  The space doe not contain any value, yet. By assigning values to the variables, you are storing the values in the space that has been reserved during the declaration.

Often, we would want to declare and assign the variables in one statement.

#include <iostream>
int main {
  int x = 1, y = 2;
  std::cout << x + y << std::endl;

  return 0;

Investigate variable types

Now that you are able to declare and assign values to variables, we shall look into the characteristics of different types of variables. From here onwards, I shall leave out the preprocessor(#include statement) and the main function’s declaration.

// Program 2.3
int x = 1, y = 2;
int z; //declares z without assigning any value
z = x + y;
std::cout << z << std::endl;
z = x - y; 
std::cout << z << std::endl;
z = x * y; // * is multiply
std::cout << z << std::endl;
z = x / y; // / is divide
std::cout << z << std::endl;
z = x % y; // % gets the remainder
std::cout << z << std::endl;

The output of Program 2.3 is



The interesting part  comes from the fourth output from the division operation. The reason why the output is 0 instead of 0.5 is because z is declared to be an integer, hence it will truncate any decimal digits. Now, modify Program 2.3 to declare z as a double.

double z;

After running, the compiler gives you the same result again. This is because both its operands are integers, hence the output will still be an integer. Now, declare x to be a double (both x and z are double). You will get an error in the last operation, the modulus % only works with integers. Since x is a double, the compiler complains this error.

Remove the modulus statement together with its std::cout statement and run. You will get 0.5 for the division operation. This lesson shows that you need to plan carefully what type of variable to be used when you write a program. Integers are generally used for counting and logic purposes. Unless you have very good reasons, use double variables to hold values.


char is a symbol that is represented internally by the computer following the ASCII code.

// Program 2.4
char x;
x = 35;
char y = 'c';
std::cout << x << std::endl;
std::cout << y << std::endl;

You get a # instead of 35. This is because x is declared as a char, hence it would be output as a char.


std::string is the type that holds multiple char between a pair of double quotes. When you declare string type, you need to prepend std:: because the string is a complex type that is defined in the std library. This is in comparison to the other native types like int, double and char.

// Program 2.5
std::string x = "This is a string. ";
std::string y = "This is another string.";
std::cout << x + y << std::endl;

In this example, you see that strings can be added together by appending one string to the other. It cannot work with other arithmetic operators however.

Getting input from user

You can declare a variable and then ask user to give it a value when the program is running. The keyword that do this is std::cin. Note that >> points to the right as compared to std::cout << which points to the left.

// Program 2.6
int x, y;
std::cin >> x; // any user input is assigned to the variable x
std::cin >> y;
std::cout << x + y std::endl;

When you run the program, your console output appears blank. It is actually waiting for you to enter a value. Try keying 5 and strike Enter. Key 3 and strike Enter. The number 8 appears.

Challenge 1

Write a program that accepts from user two inputs. Print out the value of adding, subtracting, multiplying and dividing the two numbers. Ensure that the program works correctly when the user enters either integers or doubles.