C++ Tutorial: Variables
Declare variables in C++ — types, initialization, auto, const, naming rules, and scope.
Tell the computer what to hold
In C++, every variable has a type decided at compile time. `int count = 0` means `count` holds only integers — forever. The compiler catches type mismatches before your program ever runs.
This strictness is a feature, not a bug — it eliminates entire categories of runtime errors.
What you'll learn in this C++ variables tutorial
This interactive C++ tutorial has 7 hands-on exercises. Estimated time: 16 minutes.
- Declare your first variable — Declare a variable: `type name = value;`
- Create your own variables — Declare four variables:
- Reassigning variables — Declare once with the type, then reassign without the type keyword:
- const — values that don't change — `const` variables cannot be reassigned. They MUST be initialized at declaration:
- auto — type inference — `auto` lets the compiler infer the type from the initialiser:
- Variable scope — Variables exist only within their `{...}` block — this is **scope**.
- Build a profile — No starter code. Write everything from scratch.
C++ Variables concepts covered
- Tell the computer what to hold