Jan 23, 2026
## Chapter 2: Variables and Data Types
(Variables and Data Types in C++)
The first practical thing we learn in programming
is how to store data.
In C++, this is done using Variables.
### What is a variable? (Simple Language)
Think of a variable like this:
A labelled box
- Each box has a name
- Each box can hold a specific type of data
- The data inside the box can be changed
Examples:
- A box to hold age
- Another box to hold a name
- A separate box to hold a price
### Why are variables necessary?
Because in a program:
- Every value can change repeatedly
- Every value has a different meaning
Without variables:
- the program would be hard
- confusing
- and useless
### Basic Data Types
Now let's look at some of the most common data types in C++
and understand each one in simple terms.
Example Code: Basic Data Types
C++
```
#include <iostream>
#include <string>
using namespace std;
int main() {
int years = 22; // whole number
float cost = 149.75; // decimal (less precise)
double gravity = 9.80665; // decimal (more precise)
char section = 'B'; // a single character
bool passed = true; // true or false
string studentName = "Amit"; // text
cout << years << endl;
cout << cost << endl;
cout << gravity << endl;
cout << section << endl;
cout << passed << endl;
cout << studentName << endl;
return 0;
}
```
Now let's understand each data type separately
**Integer (int):**
int is used to store whole numbers.
- It can hold values ββfrom approximately -2 billion to +2
billion
- It does not have decimals
- Best for counting purposes
Where is it used? - Number of students
- Days, months
- Count of items
Example
C++
```
int numberOfApples = 10;
```
Here:
- A variable named `numberOfApples`
- It stores the value 10
**Floating-point types (float and double):**
When we need to store numbers with decimal points,
we use `float` or `double`.
**float**
- For decimal numbers
- Less precise
- Up to approximately 7 decimal digits
- Uses less memory
C++
```
float temperature = 36.5;
```
**double**
- More accurate than float
- Up to approximately 15 decimal digits
- Used more often in real-world programs
C++
```
double accountBalance = 1543.75;
```
Rule of thumb:
If in doubt β use `double`
**Character (char):**
`char` stores only a single character or symbol.
- Always written in single quotes (' ')
- Internally, each character is stored as a number (ASCII
value)
C++
```
char firstInitial = 'J';
```
Use cases:
- Grade (A, B, C)
- Section
- Single symbol
**Boolean (bool):**
`bool` can only hold two values:
- true
- false
That is:
Yes / No
True / False
On / Off
C++
```
bool isRaining = false;
```
Use cases:
- Condition checking
- Decision making
- Loops and if-else statements
**String (string):**
`string` is used to store text.
- Holds multiple characters together
- Always in double quotes (" ")
- It is a class from the C++ standard library
For this:
C++
```
#include <string>
```
is necessary.
C++
```
string message = "Welcome to C++";
```
Use cases:
- Names
- Cities
- Messages
### Variable Naming Rules:
There are some rules for naming variables in C++. Correct
Rules
Must start with a letter (a-z, A-Z) or an underscore (_).
In the middle:
- Letters
- Numbers
- Underscores
are allowed.
- Cannot use C++ keywords.
- Case-sensitive.
`age` and `Age` are different variables.
**Good Variable Names**
C++
```
int studentAge;
double accountBalance;
string firstName;
```
- Easy to understand at a glance.
- Professional code.
**Bad (but legal) Names**
C++
```
int x; // meaning not clear
int a1b2c3; // confusing
```
The code will run,
but it will be difficult to understand.
**Illegal Variable Names**
C++
```
int 2fast; // cannot start with a number
int my-name; // hyphen not allowed
int class; // is a keyword
```
These will prevent the program from compiling.
**Beginner Tip (Golden Rule)**
Choose variable names
that make their meaning clear
without needing comments.