Ch 3 · Variables & Data Types · C++
Topic 3 of 13 in C++ — Complete Tutorial (Beginner) — 2 lessons.
Variables and Types
A variable is a named box in memory that stores a value. In C++, you declare a variable's type before using it. The common types are int (whole numbers), double (decimals), bool (true/false), char (a single character), and string (text — from the <string> library). C++ also offers auto, which lets the compiler figure out the type from the value you assign.
Constants with const
Some values should never change — like PI or a maximum limit. The const keyword locks a variable's value after you set it; trying to change it is an error the compiler catches. Using const makes your code safer by preventing accidental changes, and clearer by signalling "this value is fixed." By convention, constant names are often written in CAPITALS.