Learn C++ data types and variables with clear examples. Understand int, float, double, char, string, and bool in C++ and how variables store different kinds of data in programs.
Data means information that we store and use in a computer program.
Real-Life Examples
In a university system, we may need to store:
Different types of information require different data types.
A data type tells C++ what kind of data a variable can store.
Simple Example
Think of a variable as a box.
For example:
int age = 20;
Here:
int → Data type age → Variable 20 → Value
“int” tells C++ that “age” will store a whole number.
A variable is a named storage location used to store data in a program.
Example
int age = 20;
The variable “age” stores the value “20”.
The value of a variable can change:
int age = 20;
age = 21;
Now the value of “age” is “21”.
C++ needs to know:
For example:
int age = 20;
float cgpa = 3.5;
char grade = 'A';
bool feePaid = true;
Each variable stores a different type of information.
| Data Type | Stores | Example | Real-Life Use |
| “int” | Whole numbers | “25” | Age, marks, students |
| “float” | Decimal numbers | “5.5” | Height, temperature |
| “double” | More precise decimal numbers | “1250.75” | Fees, measurements |
| “char” | One character | “‘A’” | Grade, section |
| “string” | Text | "”Ali”” | Name, city |
| “bool” | True/False | “true” | Fee paid or not |
int — Whole NumbersUse int to store whole numbers without decimal points.
int age = 20;
int students = 500;
int marks = 85;
int usually uses 4 bytes.
Typical range:
float — Decimal NumbersUse float to store numbers with decimal values.
float temperature = 36.5;
float height = 5.4;
float weight = 65.5;
float usually uses 4 bytes.
double — More Precise Decimal Numbersdouble is used for decimal numbers when we need more precision than float.
double price = 1250.75;
double pi = 3.1415926535;
double usually uses 8 bytes.
Remember:
floatstores decimal numbers, whiledoublestores more precise decimal numbers.
char — One Characterchar stores one character.
char grade = 'A';
char section = 'B';
A char value is written inside single quotation marks:
'A'
'B'
'X'
Do not use double quotation marks for a single char:
"A" // This is a string, not a char
char uses 1 byte.
string — TextA string stores text such as words, names, and sentences.
string name = "Ali";
string university = "Ghazi University";
For strings, include:
#include <string>
string studentName = "Ali";
string city = "Dera Ghazi Khan";
string department = "Computer Science";
Remember:
charstores one character, whilestringstores multiple characters or text.
char grade = 'A';
string name = "Ali";
bool — True or Falsebool stores only two logical values:
truefalsebool isStudent = true;
bool feePaid = false;
bool isAdmitted = true;
bool feePaid = true;
bool attendanceMarked = false;
bool is useful when a program needs to represent a yes/no or true/false condition.
Different data types generally require different amounts of memory.
| Data Type | Common Memory |
|---|---|
char |
1 byte |
bool |
1 byte* |
short |
2 bytes |
int |
4 bytes |
float |
4 bytes |
double |
8 bytes |
long long |
8 bytes |
The exact size of some types can depend on the compiler and system.
Important: Students should first understand why we use different data types. Memory sizes and ranges can be learned afterward.
Imagine we are creating a University Student Record System.
string name = "Ali";
int age = 20;
float cgpa = 3.5;
char grade = 'A';
bool feePaid = true;
Each data type has a purpose:
string → Textint → Whole numberfloat → Decimal numberchar → One characterbool → True/FalseThis is why we need different data types.
Consider:
int age = 20;
age = 25.5;
age is an int, so it cannot represent 25.5 exactly.
The decimal part is discarded when the value is converted to int.
Therefore, the value becomes:
25
This demonstrates why choosing the correct data type is important.
coutcout is used to display output on the screen.
int age = 20;
cout << age;
20
We can also display text and a variable together:
cout << "Age: " << age;
Age: 20
cout << age;
Output:
20
C++ displays the value stored in the variable.
cout << "age";
Output:
age
Because "age" is text written inside quotation marks.
cincin is used to take input from the user.
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age;
If the user enters:
20
The output will be:
Your age is: 20
cin, and cout Work TogetherThe basic flow is:
Data Type
↓
Variable
↓
cin → User Input
↓
Variable stores the value
↓
cout → Display Output
For example:
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int age;
float cgpa;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Enter your CGPA: ";
cin >> cgpa;
cout << "\nStudent Information\n";
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "CGPA: " << cgpa << endl;
return 0;
}
What does this program teach?
Data Type ↓ Variable ↓ User Input using cin ↓ Value stored in variable ↓ Output using cout
A useful programming learning technique is:
«Predict the output before running the program.»
Example 1
int marks = 85;
cout « marks;
Ask yourself:
What will be displayed?
Answer:
85
Example 2
float cgpa = 3.75;
cout « cgpa;
Output:
3.75
Example 3
char grade = ‘A’;
cout « grade;
Output:
A
Data Type
«A data type tells C++ what kind of data a variable can store.»
Variable
«A variable is a named storage location used to store data.»
“int”
«Stores whole numbers.»
“float”
«Stores decimal numbers.»
“double”
«Stores decimal numbers with greater precision.»
“char”
«Stores one character.»
“string”
«Stores text.»
“bool”
«Stores “true” or “false”.»
“cin”
«Takes input from the user.»
“cout”
«Displays output on the screen.»
Think about a University Student Admission System:
Student Name → string Age → int Fee Amount → double CGPA → float Grade → char Admission Completed → bool
Complete Concept
DATA ↓ DATA TYPE ↓ VARIABLE ↓ VALUE ↓ cin → Input ↓ cout → Output
Recommended Learning Sequence
Learn these concepts in this order:
Data → Variable → Data Type → Value → Memory → “cout” → “cin” → Practical Program → Practice