Variables & Data Types

C++ is statically typed: every variable has a type that's fixed the moment it's declared, and the compiler checks that type is used consistently before your program ever runs. That's a very different deal from a language that lets a variable hold a number one moment and text the next — the trade-off is a bit more upfront ceremony in exchange for whole categories of bugs getting caught before you ship anything.

Declaring variables

A declaration states the type, then the name, and usually an initial value:

</> basics.cpp
#include <iostream>

int main() {
    int age = 29;
    double price = 19.99;
    char grade = 'A';
    bool isActive = true;

    std::cout << "Age: " << age << std::endl;
    std::cout << "Price: " << price << std::endl;
    std::cout << "Grade: " << grade << std::endl;
    std::cout << "Active: " << isActive << std::endl;
    return 0;
}
Terminal output
Age: 29
Price: 19.99
Grade: A
Active: 1

Notice isActive printed as 1, not true. By default, std::cout treats bool as the number it actually is under the hood — 0 or 1. If you want the words instead, you can write std::cout << std::boolalpha; once before printing any booleans, and every bool after that prints as true or false.

The core types

The types you'll reach for constantly:

  • int — a whole number, typically 32 bits on modern systems.
  • double — a floating-point (decimal) number, and the default choice for decimal math; float exists too but trades precision for using half the memory, which rarely matters outside of large arrays or graphics work.
  • char — a single character, stored as a small integer behind the scenes (which is why you can do arithmetic on characters).
  • booltrue or false.
  • long and long long — wider integers, for when a plain int might overflow.
  • unsigned int — an integer that can't go negative, in exchange for doubling the largest positive value it can hold.

You can check exactly how much memory a type takes on your system with sizeof:

</> sizes.cpp
#include <iostream>

int main() {
    std::cout << "int: " << sizeof(int) << " bytes" << std::endl;
    std::cout << "double: " << sizeof(double) << " bytes" << std::endl;
    std::cout << "char: " << sizeof(char) << " bytes" << std::endl;
    std::cout << "bool: " << sizeof(bool) << " bytes" << std::endl;
    return 0;
}
Terminal output
int: 4 bytes
double: 8 bytes
char: 1 byte
bool: 1 byte

Those exact numbers are true for virtually every modern 64-bit desktop and laptop, but the C++ standard only guarantees minimum sizes and relative ordering (int is at least as big as short, and so on) — it deliberately leaves the precise byte counts up to each compiler and platform, which is part of how C++ stays efficient on everything from microcontrollers to servers.

const and auto

A variable declared const can be given a value once and never reassigned — the compiler will refuse to build code that tries. It's the right choice for anything that shouldn't change after it's set, like a physical constant or a configuration value. auto asks the compiler to work out the type for you from whatever you assign, which is convenient but doesn't make C++ dynamically typed — the type is still fixed at compile time, just inferred rather than spelled out.

</> convert.cpp
#include <iostream>

int main() {
    const double freezingC = 0.0;
    auto boilingC = 100.0;  // auto deduces double, from the literal on the right

    double fahrenheit = boilingC * 9.0 / 5.0 + 32.0;
    std::cout << boilingC << "C is " << fahrenheit << "F" << std::endl;
    return 0;
}
Terminal output
100C is 212F

freezingC is declared but never used past this point in a real program — that's normal for a named constant meant to make later code self-documenting; the compiler doesn't complain about an unused const the way it would about, say, an unreachable branch.

Note: unlike some languages, C++ does not automatically initialize a plain local variable to zero. int count; without a value leaves count holding whatever garbage bits were already in that memory. Always give a variable an initial value — it costs nothing and removes an entire class of bugs that only show up intermittently.