Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Variables & Primitive Types · Lesson 22 of 25

Primitive Data Types

A free lesson from Swipe Coding · C++, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
A handful of built-in types carry almost all the data in C++. 🧮

Every variable in C++ needs a type, and the primitive (or "fundamental") types are the most basic kinds of data a variable can hold. They store a simple value directly in memory, which makes them fast and memory-efficient.

int quantity = 10;
double price = 4.99;
bool inStock = true;
char grade = 'A';
Four commonly used primitive types: a whole number, a decimal number, a true/false value, and a single character.
🤖
Four everyday types, four different jobs. 🔡

int stores whole numbers (no decimal point) and is the default choice for counting things. double stores decimal numbers and is the default choice for anything with a fractional part, like prices or measurements. bool can only ever be true or false, which makes it perfect for yes/no conditions. char stores exactly one character, written in single quotes like 'A' — never double quotes, which are reserved for std::string.

Fill in the blank

To store a single character like 'A', you use the ____ type, written with single quotes.

C++ also has smaller and larger whole-number types for when int isn't the right fit: short saves memory for small numbers, while long and long long are used for whole numbers bigger than int can reliably hold. float is a smaller, less precise alternative to double, using less memory but storing fewer accurate decimal digits.

long long population = 8000000000;
short smallNumber = 120;
float approxPi = 3.14f;
long long comfortably holds numbers far bigger than int can; float literals are marked with an f suffix.
🤖
The exact size can even depend on your computer's compiler. 💻

One quirk of C++: the exact size (in bytes) of a type like int or long is not fixed by the language the same way it is in some other languages — it can vary slightly between compilers and platforms, though int is virtually always at least 4 bytes on modern systems. If you ever need to be certain, the sizeof operator tells you the exact size in bytes on your current setup.

std::cout << sizeof(int) << std::endl; // commonly prints 4
sizeof(int) reports how many bytes an int occupies in memory on this system.
True or false

In C++, a local variable's exact byte size for a type like int is guaranteed to be identical on every computer and compiler in existence.

Quick check

Which primitive type would you use to store whether a light switch is on or off?

Chapter quiz

Variables & Primitive Types

10 questions · pass with 60% — just like in the app.

Quick check

What is a variable in C++?

Fill in the blank

The general pattern for declaring and initializing a variable in C++ is ____.

True or false

If you declare a local variable in C++ without giving it a value, C++ automatically sets it to a safe default like 0.

Quick check

Which of these is NOT a valid C++ variable name?

Fill in the blank

By convention, this course writes variable names in ____, such as totalScore.

Quick check

Which primitive type would you use to store whether a light switch is on or off?

Fill in the blank

To store a single character like 'A', you use the ____ type, written with single quotes.

True or false

double is the default choice when you need to store a number with a fractional part, such as a price.

Quick check

Which type would you reach for to store a whole number far larger than an int can reliably hold?

True or false

In C++, a type like int is guaranteed to occupy exactly the same number of bytes on every computer and compiler.

Keep your streak going in Swipe Coding

Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →