C++ Data Types: The Foundation of Logic
Mastering variables and memory allocation in modern C++.
What are Data Types?
In C++, data types tell the compiler what kind of data a variable can hold and how much memory it needs. This is critical for high-performance software engineering and memory efficiency.
Core Primitive Types
C++ provides several built-in types to handle various data formats:
int age = 24; // 4 bytes | Whole numbers
float price = 10.99f; // 4 bytes | Simple decimals
double pi = 3.14159; // 8 bytes | High-precision decimals
char grade = 'A'; // 1 byte | Single characters
string name = "Hub"; // Variable | Collection of characters
bool isElite = true; // 1 byte | Boolean logic
Type Modifiers
You can also use modifiers like unsigned, short, and long to change how much space a variable takes. For example, unsigned int only holds positive numbers, allowing it to reach higher values in the same 4-byte space.
Pro Tip: Always choose the smallest data type that can safely hold your data to optimize your program's memory footprint.
Practice Exercise: Step 2
Try to write a program that calculates the area of a circle. What data type would you use for the radius and the final result? Use double for maximum precision.