01 / 22
{x}

Variable Declaration
Built-In Types in C++

Learn to declare variables using int, double, bool, and char: the fundamental building blocks of every C++ program.

C++

What Is a Variable?

A variable is a named storage location in your computer's memory. Think of it as a labeled box: the label is the variable's name and the box holds a value you can read or change while your program runs.

age 17 value name (identifier) Memory address: 0x7ffc...

Why Types Matter

C++ is a statically typed language: you must tell the compiler what kind of data a variable will hold before you use it. The type determines how much memory is reserved and what operations are allowed.

Memory Size
An int typically uses 4 bytes; a char uses 1 byte. The type tells the compiler exactly how many bytes to reserve.
Valid Operations
You can do arithmetic on int and double, but you can't divide a bool by a char in a meaningful way.
Error Prevention
Type-checking catches bugs at compile time, long before your program runs. This saves hours of debugging.

The Four Built-In Types

int
Whole numbers (no decimals). Example: 42, -7, 0
double
Decimal (floating-point) numbers. Example: 3.14, -0.5
bool
Logical values: only true or false
char
A single character in single quotes. Example: 'A', '9'

The Declaration Syntax

type name = value ; data type identifier initial value required! (initializer is optional)
int score = 100; double gpa = 3.85; bool passed = true; char grade = 'A';

Declaration vs Initialization

You can declare a variable (create the box) without giving it a value right away. Initialization means assigning a value at the moment of declaration. Best practice: always initialize.

// Declaration only (no initial value) int count; // value is undefined! // Declaration + Initialization (preferred) int count = 0; // value is 0

Deep Dive: int

The int type stores whole numbers (integers). On most systems, it uses 4 bytes (32 bits) and can hold values from roughly \(-2.1 \times 10^9\) to \(2.1 \times 10^9\).

int age = 17; int temperature = -5; int population = 1000000; int zero = 0;

Deep Dive: double

The double type stores decimal numbers using 8 bytes (64 bits) of floating-point precision. Use it whenever you need fractions, measurements, or scientific values.

double pi = 3.14159; double price = 9.99; double tiny = 0.001; double negative = -273.15;

Deep Dive: bool

A bool holds exactly one of two values: true or false. It uses 1 byte and is essential for conditions, loops, and decision-making in your programs.

bool isStudent = true; bool hasLicense = false; bool isEven = (10 % 2 == 0); // true

Deep Dive: char

A char stores a single character using 1 byte. Characters must be enclosed in single quotes. Each character maps to a numeric ASCII code.

char letter = 'A'; // ASCII 65 char digit = '7'; // ASCII 55 char space = ' '; // ASCII 32 char newline = '\n'; // escape character

Memory Layout

int score = 100; 4 bytes double gpa = 3.85; 8 bytes bool passed = true; 1 1 byte char grade = 'A'; 65 1 byte

Step-by-Step Checklist

1
Identify the data. What kind of information do you need to store? A whole number, a decimal, true/false, or a character?
2
Choose the type. Match data to type: whole numbers → int, decimals → double, yes/no → bool, single character → char.
3
Pick a descriptive name. Use camelCase (e.g., studentAge) and avoid reserved keywords like int or return.
4
Initialize immediately. Assign a sensible starting value: int count = 0; prevents undefined behavior.
5
End with a semicolon. Every C++ statement must terminate with ; or the compiler will report an error.

Example 1: Student Profile

Declare variables to store a student's age, GPA, enrollment status, and first initial.

int age = 16; double gpa = 3.72; bool isEnrolled = true; char initial = 'J';

Example 2: Temperature Converter

Convert a temperature from Fahrenheit to Celsius using \( C = \frac{5}{9}(F - 32) \).

double fahrenheit = 98.6; double celsius = (5.0 / 9.0) * (fahrenheit - 32); // celsius is now 37.0 bool hasFever = (celsius > 37.5); // hasFever is false

Example 3: Game State

Track a player's stats in a simple game with multiple variable types working together.

int lives = 3; int score = 0; double health = 100.0; bool isAlive = true; char rank = 'B'; // After taking damage: health = health - 25.5; // now 74.5 score = score + 10; // now 10 isAlive = (health > 0); // still true

Common Mistakes

✕ Missing Semicolon
Writing int x = 5 without the trailing ; causes a compiler error on the next line, making it hard to find.
✕ Wrong Quotes for char
Using double quotes: char c = "A"; is a string literal, not a char. Use single quotes: 'A'.
✕ Integer Division Trap
Writing \(5 / 9\) gives \(0\), not \(0.555\ldots\) At least one operand must be a double: \(5.0 / 9\).
✕ Uninitialized Variables
Declaring int x; then using x before assignment reads garbage memory. Always initialize.

Type Comparison Table

Property int double bool char
Size (bytes) 4 8 1 1
Stores Whole numbers Decimals true / false 1 character
Example 42 3.14 true 'A'
Default Init Undefined (if local) Undefined (if local) Undefined (if local) Undefined (if local)
Arithmetic? ✓ ✓ Limited ✓ (ASCII)

Question 1 of 5

Which type should you use to store the value 3.14159?

Question 2 of 5

What is the output of the expression \(7 / 2\) in C++ when both operands are int?

Question 3 of 5

Which declaration is valid C++?

Question 4 of 5

How many bytes of memory does a double typically use?

Question 5 of 5

What happens if you use a variable without initializing it?