Variables, References & Basic Types

Course 1 · Ch 2
Variables, References & Basic Types
A real boolean from the start, and a second way to refer to a variable that isn't a pointer

c1-3 spent real time on C's historical lack of a true boolean. This chapter opens by closing that gap for good — then introduces something the C track never had at all.

A Genuine bool From Day One

C had no boolean type until C99, and even then, bool is a macro over _Bool — genuinely just an integer in disguise. C++ has had a real, distinct bool primitive since its very first standard. true and false are genuine keyword literals of their own type — not macros standing in for 1 and 0.

bool is_valid = true;

References — A New Concept Beyond Pointers

A reference is an alias for an existing variable — not a separate object with its own address the way a pointer is.

int x = 5; int &ref = x; // ref is another name for x, not a pointer to it ref = 10; // this changes x directly — no dereference needed

Two real, permanent constraints: a reference must be initialized the moment it's declared — there's no such thing as an uninitialized or null reference the way there's an uninitialized or null pointer — and once bound, it can never be rebound to refer to something else.

References vs. Pointers

ConceptReferencePointer
Syntax at use siteused exactly like the variable itselfrequires * to dereference
Can it be null?noyes — NULL is valid
Can it be reassigned to something else?no — bound permanently at initializationyes — freely
Must be initialized immediately?yesno — can be declared, then assigned later

cpp1-8 covers exactly when to reach for each in real code — this is just the first introduction.

auto Type Inference

auto lets the compiler deduce a variable's type from its initializer. C has no equivalent at all — every variable's type is always written explicitly. This is genuinely a point where C++ and Rust agree (Rust's let does the same thing), unlike most of the C track's own C-vs-Rust contrasts.

auto count = 5; // deduced as int auto name = "Alice"; // deduced as const char*

const in C++

Used far more pervasively and idiomatically in C++ than in C. A const variable's value cannot change after initialization — a full dedicated chapter on const-correctness comes later (cpp2-8); for now, just the basic qualifier.

const int max_size = 100; // cannot be reassigned
ConceptCC++
Boolean type_Bool/bool since C99 — really an intbool — a real, distinct primitive since the first standard
Alias for a variablenot availablereferences (&) — new in this course
Type inferencenone — always explicitauto — genuinely matches Rust's own let
Use auto where it helps, not everywhere
auto is genuinely useful for verbose or obvious types (iterator types especially, covered later) — but overusing it to the point where a reader can't tell a variable's type at a glance is a real, debated style tradeoff even within the C++ community.
A reference has no "empty" state — declaring one without binding it is a compile error
Unlike a pointer, which C happily lets you declare and assign later (or leave uninitialized, a real risk of its own), a reference must be bound the instant it's declared. int &ref; with no initializer simply doesn't compile.

Coding Challenges

Challenge 1

Declare an int variable, create a reference to it, modify the value through the reference, and print the original variable directly to confirm it changed — using cout, not printf.

📄 View solution
Challenge 2

Declare three variables using auto — an int literal, a double literal, and a string literal — and print each one's value along with, using typeid or a comment, what type auto actually deduced.

📄 View solution
Challenge 3

Explain why a reference cannot be null while a pointer can, tying your answer back to what a reference actually IS (an alias) rather than a separate object with its own address.

📄 View solution

Chapter 2 Quick Reference

  • bool/true/false — a genuine primitive and real literals, since C++'s first standard
  • int &ref = x; — an alias for an existing variable, not a separate object
  • References: no null state, no rebinding, must be initialized immediately — all unlike pointers
  • auto — type inference from the initializer, genuinely matching Rust's own let
  • const — used pervasively in idiomatic C++; full correctness rules come in cpp2-8
  • Next chapter: functions and overloading — something C structurally cannot do at all