Beginner 9 min

Variables & Data Types

Fundamental data types and how to store information in variables

Learn how to store and manipulate data using variables and understand the fundamental data types in C++.

A Simple Example

#include <iostream>

int main() {
    // Integers - whole numbers
    int age{25};
    int temperature{-15};

    // Floating-point - decimal numbers
    double price{19.99};
    double pi{3.14159};

    // Characters - single letters/symbols
    char grade{'A'};
    char symbol{'$'};

    // Boolean - true or false
    bool isStudent{true};
    bool hasLicense{false};

    // Display values
    std::cout << "Age: " << age << std::endl;
    std::cout << "Price: $" << price << std::endl;
    std::cout << "Grade: " << grade << std::endl;
    std::cout << "Is student: " << isStudent << std::endl;

    return 0;
}

Breaking It Down

int - Integer Type

  • What it stores: Whole numbers without decimals (e.g., -15, 0, 42, 1000)
  • Size: Typically 4 bytes, can hold values from about -2 billion to +2 billion
  • Use for: Counters, ages, quantities, array indices
  • Remember: Using int for decimals truncates the fractional part

double - Floating-Point Type

  • What it stores: Numbers with decimal points (e.g., 3.14159, 19.99, -0.5)
  • Size: 8 bytes with high precision (about 15-17 decimal digits)
  • Use for: Prices, measurements, scientific calculations, percentages
  • Remember: Not exact for financial calculations due to binary representation

char - Character Type

  • What it stores: Single characters in single quotes (e.g., 'A', 'x', '$', '9')
  • Size: 1 byte, can hold values from -128 to 127 (or 0 to 255 unsigned)
  • Use for: Single letters, grades, keyboard input, symbols
  • Remember: Use double quotes "" for strings, single quotes '' for chars

bool - Boolean Type

  • What it stores: Only two values - true or false
  • Size: 1 byte (though it only needs 1 bit)
  • Use for: Flags, conditions, state tracking (logged in, game over, valid)
  • Remember: 0 is false, any non-zero value is true in C++

Uniform Initialization with {}

  • What it does: Modern C++ way to initialize variables using braces
  • Benefit: Prevents narrowing conversions (e.g., double to int)
  • Syntax: int x{5}; instead of int x = 5;
  • Remember: This is the preferred initialization style in modern C++

Why This Matters

  • Variables are containers for your data - like labeled boxes in memory. Every game score, user input, sensor reading, or calculation result lives in a variable.
  • Understanding data types prevents bugs and makes your programs efficient. Using the wrong type can cause data loss, overflow errors, or wasted memory.

Critical Insight

Variable types aren't just rules - they tell the computer exactly how much memory to allocate and what operations are valid. An int is typically 4 bytes, a double is 8 bytes, and a char is just 1 byte. Choosing the right type makes your program both correct and efficient. It's like choosing the right size container for what you want to store - you wouldn't use a swimming pool to hold a cup of water, and you can't fit an ocean in a bucket.

Best Practices

Always initialize variables: Use uniform initialization like int x{0}; to avoid garbage values. Uninitialized variables contain random data from memory.

Choose the right type for the job: Use int for whole numbers, double for decimals, char for single characters, and bool for true/false values.

Use braces {} for initialization: int x{5}; is safer than int x = 5; because it prevents narrowing conversions that lose data.

Be mindful of ranges: Know the limits of your types. An int can overflow if you store values too large for it.

Common Mistakes

Using int for decimals: int price{9.99} truncates to 9, losing the cents. Use double for money.

Forgetting initialization: Uninitialized variables contain garbage values. Always initialize with {} or a value.

Overflow errors: Storing values too large for the type causes wraparound or unpredictable behavior.

char vs string confusion: Use single quotes for char ('A'), double quotes for strings ("Hello"). They are different types.

Debug Challenge

This program tries to store a decimal price. Click the highlighted line to fix the data type:

1 #include <iostream>
2
3 int main() {
4 int price{19.99};
5 std::cout << "Price: $" << price << std::endl;
6 return 0;
7 }

Quick Quiz

1. Which type should you use to store a person's height in meters (e.g., 1.75)?

double
int
char

2. What happens if you store 1000000 in a variable declared as char?

It works fine
Data loss/overflow - char can only hold -128 to 127
Compiler error

3. Which initialization style is preferred in modern C++?

int x = 5;
int x(5);
int x{5};

Practice Playground

Try out what you just learned! Write some C++ code and run it to see the results.

Lesson Progress

  • Fix This Code
  • Quick Quiz
  • Practice Playground - run once