Skip to main content
C++ Fundamentals for Beginners to Advanced
CHAPTER 04 Beginner

Variables and Data Types in C++

Updated: May 17, 2026
5 min read

# CHAPTER 4

Variables and Data Types

1. Introduction

A computer's memory is like a massive warehouse of empty boxes. To store information (like a player's score or a user's name), you need to claim a box, give it a label, and specify what kind of item goes inside. In C++, the label is the variable, and the kind of item is the data type.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Declare and initialize variables.
  • Understand primitive data types (int, float, char, bool, double).
  • Create constants using the const keyword.
  • Use the modern C++11 auto keyword.
  • Understand implicit and explicit type casting.

3. What is a Variable?

A variable is a name given to a memory location where data is stored. Its value can change (vary) during program execution.

Syntax: dataType variableName = value;

cpp
1234
int age = 25;       // 'age' holds a whole number
float price = 19.99;// 'price' holds a decimal
char grade = 'A';   // 'grade' holds a single character
bool isReady = true;// 'isReady' holds true (1) or false (0)

4. Primitive Data Types in C++

C++ is a statically typed language, meaning the compiler must know the data type of every variable before compiling.
Data TypeDescriptionSize (typical)Example
intInteger (whole number)4 bytes10, -5
floatSingle-precision decimal4 bytes3.14f
doubleDouble-precision decimal8 bytes19.99999
charSingle character1 byte'A'
boolBoolean (true/false)1 bytetrue, false

*(Note: C++ also has modifiers like unsigned int which only allows positive numbers, effectively doubling the maximum positive value).*

5. Constants (const)

Constants are variables whose values cannot be changed after initialization. Use them to protect data from accidental modification.
cpp
12
const float PI = 3.14159f;
// PI = 3.14f; // COMPILER ERROR: assignment of read-only variable

6. The auto Keyword (C++11)

Introduced in Modern C++ (C++11), the auto keyword tells the compiler to *guess* the data type based on the value assigned to it.
cpp
123
auto score = 100;     // Compiler knows this is an int
auto price = 9.99;    // Compiler knows this is a double
auto initial = 'J';   // Compiler knows this is a char

*Best Practice:* Use auto when the type is obvious or when dealing with complex STL iterators (covered in Chapter 22).

7. Type Casting (Type Conversion)

Converting data from one type to another.

Implicit Conversion (Automatic): Smaller types are safely promoted to larger types.

cpp
12
int num = 10;
double decimal = num; // Automatically becomes 10.000000

Explicit Conversion (Manual): Forcing a conversion. C++ provides modern casting operators, but C-style casting is still widely used by beginners.

cpp
123456
// C-Style Cast
double pi = 3.14159;
int rounded = (int)pi; // 'rounded' becomes 3 (truncates decimal)

// C++ Style Cast (Safer, preferred in Modern C++)
int rounded_cpp = static_cast<int>(pi);

8. Memory-Level Explanation

When you declare int age = 25;:
  1. 1. The compiler allocates 4 contiguous bytes of memory on the Stack.
  1. 2. It notes that the memory address (e.g., 0x7ffee123) is labeled age.
  1. 3. It converts 25 to binary (00011001) and stores it in those 4 bytes.

9. Common Mistakes

  • Using single quotes for strings: 'Hello' is wrong. Single quotes are ONLY for single characters ('H'). Use double quotes for strings ("Hello").
  • Forgetting f on floats: float pi = 3.14; technically creates a double and downcasts it. Use float pi = 3.14f;.
  • Uninitialized variables: int score; cout << score; prints a random "garbage" value left over in memory.

10. Exercises

  1. 1. Declare variables to store your age, height in meters (float), and blood type letter. Print them out.
  1. 2. Create a constant for the speed of light (299792458) and print it.
  1. 3. Use auto to define a variable and initialize it with true. Print it (it should print 1).

11. MCQ Quiz with Answers

Question 1

Which data type is used for storing true or false values?

Question 2

What is the size of a double on most modern 64-bit systems?

Question 3

Which of the following is a valid variable name?

Question 4

What does the auto keyword do in C++11?

Question 5

Which keyword prevents a variable from being modified?

Question 6

What happens in int x = staticcast<int>(4.99);?

Question 7

Single quotes ('A') are used for:

Question 8

What happens if you print an uninitialized variable int x; cout << x;?

Question 9

Which is the preferred way to cast in modern C++?

Question 10

What does an unsigned int do?

12. Interview Questions

  • Q: What is the difference between initialization and assignment?
  • Q: Explain implicit vs. explicit type casting.
  • Q: Why should you prefer staticcast over C-style casting (type)var in C++?

13. Summary

Variables store data in memory, requiring a specific data type like int, float, char, or bool. Constants protect data from changing, the auto keyword lets the compiler deduce types, and type casting safely converts data between types.

14. Next Chapter Recommendation

Now that you can store data, you need to manipulate it. In Chapter 5: Operators in C++, we'll cover arithmetic, relational, logical, and bitwise operators.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·