C++ Data Types β Classifying Values in C++ Programs
Introduction β Why Data Types Matter in C++
Every variable in C++ must be declared with a data type, which defines the kind of data it can hold and how much memory it consumes. Data types provide the blueprint for how values are stored and manipulated, making them crucial for writing efficient and type-safe code.
In this guide, youβll learn:
- The classification of C++ data types
- Size and range of basic types
- Syntax and usage examples
- Advanced and user-defined types
What Are Data Types in C++?
A data type tells the compiler what kind of value a variable will store, such as integers, floating-point numbers, characters, or boolean values.
int age = 30;
float salary = 55000.50;
char grade = 'A';
Categories of Data Types
C++ data types are broadly categorized into:
| Category | Includes |
|---|---|
| Primary/Built-in | int, float, char, bool, double, void |
| Derived | Arrays, Pointers, Functions, References |
| User-defined | struct, class, enum, union, typedef |
| Modifier Types | short, long, signed, unsigned |
1. Built-in Data Types
| Type | Size* | Range (Approx.) | Example |
|---|---|---|---|
int | 4 bytes | β2,147,483,648 to 2,147,483,647 | int x = 100; |
float | 4 bytes | ~Β±3.4eβ38 to Β±3.4e+38 | float pi = 3.14; |
double | 8 bytes | ~Β±1.7eβ308 to Β±1.7e+308 | double g = 9.8; |
char | 1 byte | β128 to 127 or 0 to 255 | char grade = 'A'; |
bool | 1 byte | true or false | bool isOpen = true; |
void | 0 bytes | Represents no value | void show(); |
*Size may vary depending on architecture and compiler
β³οΈ 2. Modifier Data Types
These modify the behavior of base data types:
short int si;
long int li;
unsigned int ui;
Used to save memory or extend value ranges
3. Derived Data Types
| Type | Description |
|---|---|
| Arrays | Collection of similar data types (int arr[5];) |
| Pointers | Stores address of another variable (int* ptr) |
| Functions | Return a value type (int sum(int, int);) |
| References | Alias to another variable (int &ref = a;) |
4. User-defined Data Types
Allow structuring custom data:
struct Student {
int id;
char name[30];
};
enum Color { RED, GREEN, BLUE };
Also includes:
classuniontypedefenum class(C++11)
Size & Range with sizeof
#include <iostream>
using namespace std;
int main() {
cout << "Size of int: " << sizeof(int) << " bytes\n";
cout << "Size of float: " << sizeof(float) << " bytes\n";
return 0;
}
Summary β Recap & Next Steps
Key Takeaways:
- C++ has built-in, derived, modifier, and user-defined data types
- Use appropriate data types to ensure type safety and memory efficiency
sizeof()can help determine storage needs- Modifier types extend value ranges
Real-World Relevance:
Choosing the right data type leads to optimized performance, lower memory usage, and more predictable code behavior in real-world applications.
FAQs β C++ Data Types
What is the default data type for integers?
int is the default type for whole numbers.
Can I create my own data type?
Yes. Use struct, class, union, or typedef.
What is the difference between float and double?
double has double the precision and size of float.
Whatβs the use of void type?
Indicates that a function does not return a value.
Are data type sizes fixed across all systems?
No. They can vary by system and compiler.
Share Now :
