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.

All topics in C++ Beginner

  1. Introduction to Programming & C++
  2. Your First C++ Program
  3. Variables & Data Types
  4. Input & Output
  5. Operators & Expressions
  6. Conditional Statements
  7. Loops & Iterations
  8. Functions
  9. Vectors & Strings
  10. Classes & Objects
  11. Working with Classes
  12. Exception Handling
  13. Mini-Projects & Practice