Coding/Learn/cpp/variables primitives
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnVariables & Fundamental Data Types
4 / 20 0%
Lesson 04 of 20
5 mins read

Variables & Fundamental Data Types

PreviousNext Lesson
In Plain English

A variable is a named storage container in computer memory (RAM). Because C++ is statically typed, you must declare the exact type of data a variable will hold before using it: integers (int), decimals (double), single characters (char), truths (bool), or text strings (std::string).

The Mental Model

"Think of variables as labeled toolboxes. An "int" box is shaped specifically to hold solid integer bricks. You cannot pour milk (double) into the brick box without causing a mess, and you can't put a book (string) in it either!"

Deep Dive: How It Works

int (Integer): Whole numbers without decimals (e.g. 42, -15, 0). Typically 4 bytes (32 bits).

double & float: Floating-point decimal numbers. double gives 64-bit precision (e.g. 3.14159) and is the default for decimals.

char: A single character enclosed in single quotes (e.g. 'A', '$', '9'). 1 byte.

bool: Boolean truth value (true or false). Typically 1 byte.

std::string: Text string enclosed in double quotes (e.g. "ZenCompiler"). Requires #include <string>.

const: Marks a variable as read-only/immutable (e.g. const double PI = 3.14159;).

Syntax Blueprint

int age = 21;          // 4 bytes integer
double price = 19.99;  // 8 bytes decimal
char grade = 'A';      // 1 byte character
bool isReady = true;   // 1 byte boolean
std::string user = "Neo";

Syntax: <type> <variable_name> = <initial_value>;

PointCore Rules to Remember

Point star
Static Typing: Variable types are fixed at compile time and cannot change randomly.
Point star
Single vs Double Quotes: Single quotes ('Z') are for char; double quotes ("Zen") are for strings.
Point star
Always Initialize: Uninitialized variables contain unpredictable garbage data left in RAM!

Common Beginner Traps & How to Fix Them

Mistake:Using double quotes for a char: char grade = "A";

Why it happens: Treating characters like 1-letter strings.

How to fix: Use single quotes for char: char grade = 'A';.

Pro Tip: Use const on variables whose values should never change during program execution (like TAX_RATE = 0.08).

Live Interactive Example

Hit Run Code to see it live
Declaring and Printing Primitives
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: std::boolalpha prints booleans as "true"/"false" instead of 1/0.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Declare an integer and double

  • PointDeclare an int variable named fps set to 60.
  • PointDeclare a double variable named latency set to 1.5.
  • PointPrint "FPS: 60 | Latency: 1.5ms" using these variables.
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates