Introduction
Every value in C++ has a type, and the compiler checks your use of each one before the program ever runs. Catching a mistake at that stage is much cheaper than finding it in front of a user, and it is one of the reasons C++ is used for software that must be reliable.
How it works
| Type | Holds | Example |
|---|---|---|
int | whole numbers | 42 |
long long | very large whole numbers | 9000000000LL |
double | numbers with a fraction | 3.14 |
char | one character | 'A' |
bool | true or false | true |
string | text | "hello" |
Give every variable a value. A local int that has never been assigned holds whatever was left in that memory. Using it is a bug.
const promises that a value will not change, and the compiler enforces it. It documents your intent and catches accidental changes.
auto asks the compiler to work out the type from the value it starts with. It is the same type as if you had written it, and it is checked just as strictly.
Integer division. When both sides of / are whole numbers, C++ drops the fraction, so 7 / 2 is 3. This is the most common arithmetic surprise.
static_cast<T>(x) converts a value on purpose. Older code writes (double)x, but static_cast is easier to spot and is stricter about what it allows.
#include <iostream>
using namespace std;
int main() {
int a = 7, b = 2;
cout << a / b << endl;
cout << static_cast<double>(a) / b << endl;
cout << a % b << endl;
double x = 3.9;
int whole = static_cast<int>(x);
cout << whole << endl;
return 0;
}cout prints true as 1 unless you ask for words, using boolalpha.
Syntax
int n = 0;
double d = 2.5;
const double RATE = 0.15;
auto ratio = d / 2;
double exact = static_cast<double>(n) / 3;
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
int count = 3;
double price = 19.5;
char grade = 'A';
bool paid = true;
string item = "Pen";
const int LIMIT = 10;
auto total = count * price;
cout << item << " " << grade << " " << paid << endl;
cout << total << " " << LIMIT - count << endl;
return 0;
}Line by line
- Five variables are created, each with a different type.
const int LIMITcan never be given a new value.auto total = count * price;is adouble, because an int times a double is a double.- The first
coutprints theboolas 1. - The last line prints the total and how far the count is below the limit.
Expected result
Pen A 1
58.5 7
In the real world
Choosing a type is an engineering decision. Banking software avoids double for money, because decimals are approximate, and games use small types to fit millions of objects in memory.
Common mistakes
- Using a variable before giving it a value.
- Dividing two ints and expecting a fraction.
- Mixing up
'A'and"A". The first is a character, and the second is a string. - Letting an
intoverflow. Uselong longfor large products.
Best practices
- Use
constby default for values that will not change. - Give every variable a first value where you create it.
- Use
autowhen the type is obvious from the right-hand side.
Summary
Every variable has a type that is checked by the compiler. const prevents changes, auto works out the type for you, and static_cast converts on purpose. Dividing two ints drops the fraction.
Quick check
Key takeaways
- Dividing two ints gives an int.
- Use const whenever a value should not change.
- Give every variable a value when you create it.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.