Variables in C++
A variable in C++ is a named slot of memory with a fixed type decided at declaration — int score = 0; reserves integer storage and binds the name score to it. Unlike Python, the type can never change afterward: assigning a string to an int variable is a compile error, not a runtime surprise.
Declaration and initialization are separate ideas worth keeping distinct. int x; declares without a value (built-in types are left with indeterminate garbage, not zero), while int x = 5;, int x{5};, and int x(5) all initialize immediately — and initializing immediately is the habit that prevents a whole class of bugs.
Declaration fixes the type
int score; tells the compiler score will always be an int. Its type is locked in for the rest of its scope.
Uninitialized means garbage
Built-in types declared without a value hold whatever bits were in that memory — reading them is undefined behavior, not a clean zero.
Prefer immediate initialization
int score = 0; (copy), int score{0}; (braces, rejects narrowing), and int score(0); all start life with a known value.
Braced init blocks narrowing
int x{3.99}; refuses to compile because it would silently drop .99, while int x = 3.99; compiles with a warning — braces are the safer default.
#include <iostream> int main() { int score{0}; score = 42; std::cout << "score=" << score << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.