Variables, References & Basic Types
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.
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.
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
| Concept | Reference | Pointer |
|---|---|---|
| Syntax at use site | used exactly like the variable itself | requires * to dereference |
| Can it be null? | no | yes — NULL is valid |
| Can it be reassigned to something else? | no — bound permanently at initialization | yes — freely |
| Must be initialized immediately? | yes | no — 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.
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.
| Concept | C | C++ |
|---|---|---|
| Boolean type | _Bool/bool since C99 — really an int | bool — a real, distinct primitive since the first standard |
| Alias for a variable | not available | references (&) — new in this course |
| Type inference | none — always explicit | auto — genuinely matches Rust's own let |
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.
int &ref; with no initializer simply doesn't compile.
Coding Challenges
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 solutionDeclare 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 solutionExplain 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 solutionChapter 2 Quick Reference
bool/true/false— a genuine primitive and real literals, since C++'s first standardint &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 ownletconst— used pervasively in idiomatic C++; full correctness rules come incpp2-8- Next chapter: functions and overloading — something C structurally cannot do at all