Last Updated:

Variables and Data Types in C++ - Lesson 2

From a high school math course, we all know what variables are. In programming, the principles are quite similar. A variable is a "cell" of a computer's RAM that can store any information.

In programming, a variable, as in mathematics, can have a name consisting of a single Latin letter, but can also consist of several characters, a whole word, or several words.

Data Types


In C++, all variables have a specific data type. For example, a variable of an integer type cannot contain anything but integers, and a floating-point variable can contain only fractional numbers.

A data type is assigned to a variable when it is declared or initialized. Here are the main C++ data types we'll need:

Basic Data Types in C++

  • int is an integer data type.
  • float is a floating-point data type.
  • double is a double-precision floating-point data type.
  • char is a character data type.
  • bool is a Boolean data type.

Declaring a Variable

Declaring a variable in C++ is as follows: first, you specify the data type for that variable, and then you specify the name of the variable.

Example of declaring variables

int a; // declaration of variable a of integer type.
float b; // declaration of variable b of floating point data type.
double c = 14.2; // initialization of a variable of double type.
char d = 's'; // initialization of a variable of type char.
bool k = true; // initialization of the boolean variable k.
  • Note that in C++, the assignment operator (=) is not an equal sign and cannot be used to compare values. The equality operator is written as "double equal" — .==

  • Assignment is used to store a specific value in a variable. For example, a type record sets a variable to the value of the number 10.a = 10a

Simple calculator in C++

Now we will write a simple calculator program that will take two integers from the user, and then determine their sum:

#include <iostream>
using namespace std;

int main()
     setlocale(0, "");
     /*7*/ int a, b; // declaration of two variables a and b of an integer data type.
     cout << "Enter the first number: ";
     cin >> a; // the user assigns a value to the variable a.
     cout << "Enter the second number: ";
     cin >> b;
     /*12*/ int c = a + b; // new variable c is assigned the value of the sum of the data entered by the user.
     cout << "Sum of numbers = " << c << endl; // output the response.
     return 0;

Code Parsing

In the 7th line of code of the program, we declare variables and an integer of type . The next line of code displays a message to the user to type the first number.«a»«b»int

In the 9th line there is a still unfamiliar design to you - . It prompts the user to enter the value of a variable from the keyboard. Similarly, the value of the variable .cin >>«a»«b»

In the 12th line, we initialize the variable with the sum of the variables and . Next is the already familiar operator , which displays the string and value of the variable .«c»«a»«b»cout«c»

  • When you output variables, they are not enclosed in quotation marks, unlike strings.


Try to conduct several experiments with the program - make a similar example with multiplying or subtracting variables. Do not be afraid to mock the program code, because mistakes are an integral part of learning any business. And don't forget the semicolons.