Skip to content
Educora
Beginner16 min2 / 10

Variables, types and operators

Declare variables, work with `int`, `double`, `char`, `bool`, `std::string` and `auto`, and learn arithmetic operators and input with `std::cin`.

Check yourself
In this lesson you will learn
  • Declare variables of different types and give them values
  • Use the auto and const keywords
  • Apply arithmetic operators and explain integer division and %
  • Read data with std::cin and control how decimals are printed

In a game, a character's health, score and name must be remembered: the score grows, health drops and the name stays the same. A program keeps such data in variables. In C++ every variable has its own type, and it is fixed at compile time, before the program ever runs.

Variables and types

Definition
Variable

A named place in memory that holds a value of a particular type. In C++ a variable is declared as type name = value;, for example int age = 14;.

TypeWhat it storesExample
intwhole numbers42, -7
doublenumbers with a fractional part3.14, -0.5
chara single character, in single quotes'A', '7'
boola truth valuetrue, false
std::stringtext, in double quotes"Baku"
long longvery large whole numbers9000000000
The main types in C++
C++
#include <iostream>
#include <string>

int main() {
    int age = 14;
    double height = 1.62;
    char grade = 'A';
    bool isStudent = true;
    std::string city = "Baku";

    std::cout << "Age: " << age << '\n';
    std::cout << "Height: " << height << " m\n";
    std::cout << "Grade: " << grade << '\n';
    std::cout << "Student: " << isStudent << '\n';
    std::cout << "City: " << city << '\n';
    return 0;
}
Expected output
Age: 14
Height: 1.62 m
Grade: A
Student: 1
City: Baku

Notice that a char value is written in single quotes ('A') and a std::string in double quotes. std::string needs #include <string>. A bool value is printed as 1 (true) or 0 (false).

A computer stores every value in binary, as bits. An int usually takes 4 bytes (32 bits) and can hold numbers from about −2.1 billion to 2.1 billion. For bigger numbers there is the long long type. Toggle the bits below and see which numbers 8 bits can represent.

Interactive
Loading simulation…
8 bits give 256 different values, from 0 to 255.

auto and const

If the type is obvious from the initial value, you can leave it out: the auto keyword asks the compiler to work out the type itself. auto does not make a variable typeless — the type is still chosen once, at compile time. const turns a variable into a constant: after it gets its first value, it cannot be changed.

C++
#include <iostream>

int main() {
    auto lives = 3;       // int
    auto speed = 2.5;     // double
    auto letter = 'Z';    // char
    const int maxLives = 5;

    lives = lives + 1;
    std::cout << "Lives: " << lives << " of " << maxLives << '\n';
    std::cout << "Speed: " << speed << ", letter: " << letter << '\n';
    // maxLives = 6;      // error: maxLives is const
    return 0;
}
Expected output
Lives: 4 of 5
Speed: 2.5, letter: Z

Arithmetic operators

C++ knows the usual arithmetic: +, -, *, /. There is one subtlety: when you divide two whole numbers, the result is also whole — the fractional part is simply dropped. The % operator gives the remainder of the division. If you need a fractional result, one of the numbers must be a double — for example, write 17.0 or convert the type with static_cast<double>.

C++
#include <iostream>

int main() {
    int a = 17;
    int b = 5;
    std::cout << "a / b = " << a / b << '\n';
    std::cout << "a % b = " << a % b << '\n';
    std::cout << "17.0 / 5 = " << 17.0 / 5 << '\n';
    std::cout << "cast: " << static_cast<double>(a) / b << '\n';
    return 0;
}
Expected output
a / b = 3
a % b = 2
17.0 / 5 = 3.4
cast: 3.4

There are shortcuts for changing a variable: score += 5 means score = score + 5, and -=, *=, /= work the same way. score++ increases the value by 1 and score-- decreases it by 1.

C++
#include <iostream>

int main() {
    int score = 10;
    score += 5;    // 15
    score *= 2;    // 30
    score -= 4;    // 26
    score++;       // 27
    std::cout << "Score: " << score << '\n';

    int coins = 7;
    coins /= 2;    // 3
    std::cout << "Coins: " << coins << '\n';
    return 0;
}
Expected output
Score: 27
Coins: 3

Output and input

By default std::cout prints fractional numbers with at most 6 significant digits. To choose how many digits appear after the decimal point, include <iomanip> and use std::fixed with std::setprecision:

C++
#include <iomanip>
#include <iostream>

int main() {
    double total = 14.0;
    double average = total / 3;
    std::cout << average << '\n';
    std::cout << std::fixed << std::setprecision(2) << average << '\n';
    return 0;
}
Expected output
4.66667
4.67

To read data from the keyboard, use std::cin and the >> operator: std::cin >> year; stores the number the user types in the variable year. In this course's examples we set values in the code so that the result is always the same. You can try the fragment below on your own computer:

C++
std::string name;
int year = 0;
std::cout << "Your name: ";
std::cin >> name;
std::cout << "Birth year: ";
std::cin >> year;
std::cout << "Hi, " << name << "! In 2030 you will be " << 2030 - year << ".\n";
If you enter Murad and 2012, the program prints: Hi, Murad! In 2030 you will be 18.

Key points

  • A variable is declared as type name = value; and its type never changes afterwards.
  • The core types are int, double, char, bool and std::string.
  • auto deduces the type from the initial value; const forbids changing the value.
  • Dividing two int values gives a whole number; % finds the remainder.
  • std::cin >> reads from the keyboard; std::fixed and std::setprecision control how decimals are printed.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
After int x = 17 / 5;, what is x?