- Declare variables of different types and give them values
- Use the
autoandconstkeywords - Apply arithmetic operators and explain integer division and
% - Read data with
std::cinand 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
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;.
| Type | What it stores | Example |
|---|---|---|
int | whole numbers | 42, -7 |
double | numbers with a fractional part | 3.14, -0.5 |
char | a single character, in single quotes | 'A', '7' |
bool | a truth value | true, false |
std::string | text, in double quotes | "Baku" |
long long | very large whole numbers | 9000000000 |
#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;
}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.
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.
#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;
}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>.
#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;
}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.
#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;
}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:
#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;
}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:
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";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,boolandstd::string. autodeduces the type from the initial value;constforbids changing the value.- Dividing two
intvalues gives a whole number;%finds the remainder. std::cin >>reads from the keyboard;std::fixedandstd::setprecisioncontrol how decimals are printed.
Check yourself
10 questions. Every correct answer earns XP.
int x = 17 / 5;, what is x?