Variables and Data Types in C++
# 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
constkeyword.
-
Use the modern C++11
autokeyword.
- 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;
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 Type | Description | Size (typical) | Example |
|---|---|---|---|
int | Integer (whole number) | 4 bytes | 10, -5 |
float | Single-precision decimal | 4 bytes | 3.14f |
double | Double-precision decimal | 8 bytes | 19.99999 |
char | Single character | 1 byte | 'A' |
bool | Boolean (true/false) | 1 byte | true, 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.
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.
*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.
Explicit Conversion (Manual): Forcing a conversion. C++ provides modern casting operators, but C-style casting is still widely used by beginners.
8. Memory-Level Explanation
When you declareint age = 25;:
- 1. The compiler allocates 4 contiguous bytes of memory on the Stack.
-
2.
It notes that the memory address (e.g.,
0x7ffee123) is labeledage.
-
3.
It converts
25to 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
fon floats:float pi = 3.14;technically creates a double and downcasts it. Usefloat pi = 3.14f;.
-
Uninitialized variables:
int score; cout << score;prints a random "garbage" value left over in memory.
10. Exercises
- 1. Declare variables to store your age, height in meters (float), and blood type letter. Print them out.
- 2. Create a constant for the speed of light (299792458) and print it.
-
3.
Use
autoto define a variable and initialize it withtrue. Print it (it should print 1).
11. MCQ Quiz with Answers
Which data type is used for storing true or false values?
What is the size of a double on most modern 64-bit systems?
Which of the following is a valid variable name?
What does the auto keyword do in C++11?
Which keyword prevents a variable from being modified?
What happens in int x = staticcast<int>(4.99);?
Single quotes ('A') are used for:
What happens if you print an uninitialized variable int x; cout << x;?
Which is the preferred way to cast in modern C++?
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
staticcastover C-style casting(type)varin C++?
13. Summary
Variables store data in memory, requiring a specific data type likeint, 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.