Chapter 3: Variables & Data Types

Variables & Data Types in C++: The Building Blocks of Your Programs

Hello again, my eager student! 🎉 You’ve already written your first “Hello World” program — that’s fantastic! Now we’re moving to the heart of programming: variables and data types. Think of variables as little labeled boxes where you store information (numbers, text, true/false values, etc.), and data types tell the computer what kind of thing is inside each box.

Today we’ll cover:

  • Primitive (basic) data types — the most common ones you’ll use every day
  • The modern auto keyword — super useful and beginner-friendly
  • Constants (const and constexpr) — values that never change

We’ll go slow, with lots of examples, explanations, common mistakes, and small programs you can type and run right now.

1. What is a Variable?

A variable is a named storage location in memory that holds a value. You give it:

  • A name (you choose — make it meaningful!)
  • A type (what kind of data it can hold)
  • A value (optional at first — you can set it later)

Basic syntax to declare a variable:

C++

Example:

C++

2. Primitive (Basic) Data Types in C++

C++ has several built-in primitive types. Here are the most important ones for beginners:

Type What it stores Size (usually) Example values Notes
int Whole numbers (integers) 4 bytes -5, 0, 42, 1000000 Most common for counting, indices
float Decimal numbers (floating-point) 4 bytes 3.14, -0.001, 9.8765 Less precise than double
double Decimal numbers (higher precision) 8 bytes 3.141592653589793, 1.23456789 Use this for most real numbers
char Single character 1 byte ‘A’, ‘z’, ‘5’, ‘$’, ‘\n’ Use single quotes ‘ ‘
bool True or false 1 byte true, false Used in conditions
std::string Text (sequence of characters) Varies “Hello”, “C++ is fun!”, “” From <string> header

Let’s see them in action!

Create a new file variables.cpp and try this program:

C++

Output (something like):

text

Important notes:

  • Use double almost always instead of float — it has much better precision.
  • std::string is not a primitive type in the strict sense, but it behaves like one and is used everywhere.

3. The Amazing auto Keyword (C++11 and later)

Since C++11, you can let the compiler automatically deduce (figure out) the type for you using auto.

C++

Modern best practice (very common in 2025/2026):

C++

When to use auto?

  • Almost always when the type is obvious from the right-hand side
  • Especially with long/complex types (we’ll see this more with containers later)

When NOT to use auto?

  • When the type itself is important documentation:
    C++

4. Constants: Values That Never Change

Sometimes you want a value that cannot be changed after it’s set. C++ gives you two main ways:

A. const – Constant variable (value cannot change after initialization)

C++

Very common pattern:

C++
B. constexpr – Compile-time constant (even stronger!)

constexpr means the value is known at compile time and can be used in places that need constant expressions (array sizes, case labels in switch, etc.).

C++

Modern recommendation (2025+): Use constexpr whenever possible — it’s safer and allows more optimizations.

Example combining everything:

C++

5. Quick Summary Table

Feature Keyword(s) When to use Example
Basic integer int Counting, indices int count = 0;
Floating point double (preferred) Real-world measurements double salary = 125000.50;
Text std::string Names, messages string name = “Webliance”;
True/False bool Conditions bool isLoggedIn = true;
Auto type deduction auto When type is obvious auto result = calculate();
Constant (runtime) const Values that shouldn’t change const int MAX_USERS = 1000;
Constant (compile) constexpr Compile-time constants constexpr int SIZE = 100;

6. Your Mini Homework (Try These!)

  1. Create variables for your name, age, height (in meters), and whether you like C++ (bool).
  2. Print them nicely formatted.
  3. Make PI a constexpr and calculate the area of a circle with radius 7.5.
  4. Try declaring a variable with auto and print its value.

Example starter:

C++

You’re doing amazing! Variables and types are the foundation — once you’re comfortable here, loops, conditions, and functions will feel much easier.

Any questions? Confused about float vs double? Want to see more examples? Just ask — I’m your C++ teacher and I’m here for you! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *