Variables and types in C++

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

TypeHoldsExample
intwhole numbers42
long longvery large whole numbers9000000000LL
doublenumbers with a fraction3.14
charone character'A'
booltrue or falsetrue
stringtext"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;
}
Open in compiler

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;
}
Open in compiler

Line by line

  1. Five variables are created, each with a different type.
  2. const int LIMIT can never be given a new value.
  3. auto total = count * price; is a double, because an int times a double is a double.
  4. The first cout prints the bool as 1.
  5. 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 int overflow. Use long long for large products.

Best practices

  • Use const by default for values that will not change.
  • Give every variable a first value where you create it.
  • Use auto when 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

1. What is 9 / 2 when both are int?
2. Which line gives 4.5?
3. What is the type of x in auto x = 2.5;?
4. What happens if code changes a const variable?

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.