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:
|
0 1 2 3 4 5 6 7 |
type variableName; // Just declare (value not set yet) type variableName = value; // Declare and initialize |
Example:
|
0 1 2 3 4 5 6 |
int age = 25; // age is a variable of type int, holding 25 |
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:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <iostream> #include <string> // Required for std::string using namespace std; int main() { // Integer int age = 22; int score = -10; // Can be negative // Floating-point numbers float pi_float = 3.14159f; // Notice the 'f' at the end double pi_double = 3.141592653589793; // Character char grade = 'A'; char newline = '\n'; // Special character: new line // Boolean bool isStudent = true; bool hasLicense = false; // String string name = "Webliance"; string greeting = "Hello, "; // Print everything cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Score: " << score << endl; cout << "Pi (float): " << pi_float << endl; cout << "Pi (double): " << pi_double << " (more precise!)" << endl; cout << "Grade: " << grade << endl; cout << "Is student? " << (isStudent ? "Yes" : "No") << endl; cout << greeting << name << "!" << newline; return 0; } |
Output (something like):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
Name: Webliance Age: 22 Score: -10 Pi (float): 3.14159 Pi (double): 3.14159 (more precise!) Grade: A Is student? Yes Hello, Webliance! |
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.
|
0 1 2 3 4 5 6 7 8 9 |
auto x = 42; // x is int auto y = 3.14; // y is double auto name = "Alice"; // name is const char* (but we usually want string) auto isCool = true; // isCool is bool |
Modern best practice (very common in 2025/2026):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> using namespace std; int main() { auto age = 25; // int auto pi = 3.141592653589793; // double auto message = string("Hello"); // std::string (recommended!) auto isActive = true; // bool cout << age << " " << pi << " " << message << " " << isActive << endl; return 0; } |
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++0123456double temperature = 36.6; // We want to make it clear it's a floating-point number
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)
|
0 1 2 3 4 5 6 7 8 9 |
const double PI = 3.141592653589793; const int MAX_SCORE = 100; // PI = 3.2; // ERROR! Cannot modify const |
Very common pattern:
|
0 1 2 3 4 5 6 |
const string SCHOOL_NAME = "Hyderabad Institute of Technology"; |
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.).
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
constexpr int DAYS_IN_WEEK = 7; constexpr double TAX_RATE = 0.18; // Can be used like this: int workingDays[DAYS_IN_WEEK]; // OK because DAYS_IN_WEEK is constexpr // But this is NOT allowed with just const: const int userInput = 10; // Value not known at compile time // int arr[userInput]; // ERROR in most cases |
Modern recommendation (2025+): Use constexpr whenever possible — it’s safer and allows more optimizations.
Example combining everything:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <string> using namespace std; int main() { constexpr double PI = 3.141592653589793; const string APP_NAME = "My First C++ App"; auto radius = 5.0; // auto → double auto area = PI * radius * radius; // calculated at runtime cout << APP_NAME << endl; cout << "Area of circle (r = " << radius << "): " << area << endl; cout << "Using PI = " << PI << endl; // radius = 10; // OK // PI = 3.2; // ERROR - const/constexpr return 0; } |
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!)
- Create variables for your name, age, height (in meters), and whether you like C++ (bool).
- Print them nicely formatted.
- Make PI a constexpr and calculate the area of a circle with radius 7.5.
- Try declaring a variable with auto and print its value.
Example starter:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> using namespace std; int main() { // YOUR CODE HERE return 0; } |
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! 🚀
