Variables & Data Types
Meet C++ β C's Evolved Sibling
If C is the rugged pioneer who built the roads, C++ is the sibling who paved them, added lane markings, and installed guardrails. They share the same DNA β pointers, manual memory, compiled speed β but C++ learned from C's rough edges and added better types, safer defaults, and modern conveniences.
Nowhere is this more obvious than in how you declare variables. C makes you spell everything out. C++ says: "I can figure most of that out for you."
The Fundamental Types
C++ inherits all of C's numeric types and adds a few quality-of-life upgrades:
intβ whole numbers:42,-7,0. Typically 32 bits.doubleβ decimal numbers with high precision:3.14159. This is your default for floating-point math.floatβ less precise decimals, uses half the memory ofdouble. Use when memory matters (like graphics).charβ a single character:'A','7','\n'. Under the hood, it's just a small integer.boolβtrueorfalse. In C, you needed#include <stdbool.h>β in C++, bool is a native, first-class type. No header needed!std::stringβ a real string type! No more jugglingchar[]arrays andstrlen(). It grows, shrinks, and manages its own memory.
Basic Declarations β C++ Style
The auto Keyword β Let the Compiler Do the Work
Imagine you're at a restaurant and the waiter already knows your order because you come every day. That's auto β you skip the type name, and the compiler deduces it from whatever you assign.
auto was introduced in C++11 and quickly became one of the most-used features. It's especially handy when the type name is long and ugly (like iterators), but it works with simple types too.
auto in Action
auto doesn't mean "untyped" β the compiler figures out the exact type at compile time. It's still strongly typed! Once deduced, the type is locked in. auto x = 5; makes x an int, and you can't later assign a string to it. Think of auto as shorthand, not as JavaScript's var.const vs constexpr β Two Flavors of "Don't Touch"
C has const, and C++ keeps it β but adds constexpr for compile-time constants. Think of it this way:
constβ "This value won't change at runtime." The value might be computed when the program runs, but once set, it's locked.constexprβ "This value is known at compile time." The compiler computes it before the program even runs, which means it can be used in places that require compile-time constants (like array sizes).
const vs constexpr
Uniform Initialization with {}
C++ has a frustrating history of too many ways to initialize things. C++11 introduced uniform initialization using curly braces {} to bring order to the chaos. The key benefit? It prevents narrowing conversions β silent data loss that C happily allows.
Think of {} as the strict parent: it won't let you silently stuff a double into an int and lose the decimals.
Uniform Initialization with {}
C++ string vs C's char[]
In C, strings are just arrays of characters ending with a null byte '\0'. You had to manually manage lengths, allocate buffers, and pray you didn't overrun them. C++'s std::string handles all of that for you β it grows automatically, knows its own length, and supports comparison with == instead of strcmp().