Lesson 5
Data Types
Covers fundamental, derived, user-defined types.
Lesson content
What are data types?
Data type sets what a variable stores.
It tells compiler memory size and allowed ops.
Int stores whole numbers. Float stores decimals. Char stores one letter.
Think container for a specific item type.
Why do we need data types?
- Defines value type a variable holds
- Helps compiler allocate right memory
- Prevents invalid operations
- Improves performance and memory use
- Makes programs easier to maintain
Classification of data types
Data Types
│
┌──────────────────┼──────────────────┐
│ │ │
▼ ▼ ▼
Fundamental Derived Data Types User-Defined
Data Types Data Types
│ │ │
▼ ▼ ▼
int Arrays class
char Pointers struct
float References union
double Functions enum
bool typedef
void usingFundamental (built-in) data types
Data Type | Description | Typical Size | Example
--------- | ------------------------------------- | ------------ | ---------------------
int | Stores whole numbers | 4 bytes | 25
float | Stores decimal numbers | 4 bytes | 3.14
double | Stores high-precision decimal numbers | 8 bytes | 99.999
char | Stores a single character | 1 byte | 'A'
bool | Stores true or false | 1 byte | true
void | Represents no value | — | Function return typeSize varies by compiler and OS.
Integer (int)
Int stores whole numbers, no decimals.
#include <iostream>
using namespace std;
int main()
{
int age = 20;
cout << age << endl;
return 0;
}Floating-point (float)
Float stores decimal numbers.
#include <iostream>
using namespace std;
int main()
{
float price = 99.99f;
cout << price << endl;
return 0;
}Double (double)
Double gives more precision than float.
#include <iostream>
using namespace std;
int main()
{
double pi = 3.14159265358979;
cout << pi << endl;
return 0;
}Character (char)
Char stores one character in single quotes.
#include <iostream>
using namespace std;
int main()
{
char grade = 'A';
cout << grade << endl;
return 0;
}Boolean (bool)
Bool stores only true or false.
#include <iostream>
using namespace std;
int main()
{
bool isPassed = true;
cout << isPassed << endl;
return 0;
}Output shows 1 for true.
Void (void)
Void means no returned value.
#include <iostream>
using namespace std;
void greet()
{
cout << "Welcome!" << endl;
}
int main()
{
greet();
return 0;
}Derived data types
Built from fundamental types.
- Arrays
- Pointers
- References
- Functions
User-defined data types
Programmers build custom structures.
- Class
- Structure (struct)
- Union (union)
- Enumeration (enum)
- Type aliases (typedef, using)
Data type modifiers
Modifiers change size or range.
Modifier | Example
-------- | --------------
short | short int
long | long int
signed | signed int
unsigned | unsigned int#include <iostream>
using namespace std;
int main()
{
unsigned int population = 100000;
cout << population << endl;
return 0;
}Common mistakes and pitfalls
- Int for decimals. Fix: use float or double
- Double quotes for char. Fix: use single quotes
- Large values in float. Fix: use double
- Assuming fixed sizes. Fix: sizes vary by platform
// Incorrect
char grade = "A";// Correct
char grade = 'A';Best practices
- Use int for whole numbers
- Prefer double over float generally
- Use bool for logical values
- Choose descriptive variable names
- Pick smallest fitting data type
When not to use certain data types
- Float for money. Use double or fixed-point
- Char for words. Use std::string
- Int for huge values. Use long long
- Double for true/false. Use bool
Summary
- Data type defines what a variable stores
- Types split into fundamental, derived, user-defined
- Fundamental: int, float, double, char, bool, void
- Derived: arrays, pointers, references, functions
- User-defined: classes, structs, unions, enums
- Right type improves memory and readability
Frequently asked questions
What is a data type in C++?
Defines value kind and memory needed.
What is the difference between float and double?
Double holds more precision than float.
Why is char enclosed in single quotes?
Single quotes mark one char. Double quotes mean string.
What is the purpose of the bool data type?
Stores true or false for conditions.
What should I learn after data types?
- Variables
- Constants
- Type Modifiers
- Type Casting
- Operators
- Input and Output
- Control Statements