1. Intro
A data type tells the compiler what kind of value a variable will hold and how much memory to set aside for it. C++ is a statically typed language, so every variable's data type must be known before the program runs. Data types are broadly grouped into fundamental (built-in) types, derived types (arrays, pointers, functions), and user-defined types (classes, structs, enums).
Cricket analogy: A data type is like a team roster slot pre-declared as 'bowler', 'batsman' or 'all-rounder' before the squad is even announced — C++ similarly demands every variable's role be fixed before the match runs, grouped into basic roles, combo roles (derived types), and specialist categories (user-defined types).
2. Syntax
data_type variable_name;3. Explanation
The fundamental data types built directly into C++ are:
Cricket analogy: Just as a scorecard has a fixed set of built-in categories — runs, wickets, overs — C++ comes with its own fixed set of fundamental data types built directly into the language.
- int — stores whole numbers (e.g. 10, -42)
- float — stores single-precision decimal numbers (e.g. 3.14f)
- double — stores double-precision decimal numbers, more precise than float
- char — stores a single character (e.g. 'A')
- bool — stores true or false
- void — represents "no value", commonly used as a function return type
The exact size in bytes for each type is implementation-defined by the compiler and target platform rather than fixed by the C++ standard — but on most common modern compilers, int is typically 4 bytes, char is 1 byte, float is 4 bytes, and double is 8 bytes. You can always check the size on your own system using the sizeof operator.
Cricket analogy: Just as boundary rope distances vary slightly ground to ground even though roughly '70 yards' is typical, C++ type sizes like int (typically 4 bytes) aren't fixed by the standard but by the compiler and platform — you check the actual value with sizeof, like measuring the actual rope at your specific stadium.
Pro Tip
use sizeof(type) to check exactly how many bytes a data type occupies on your specific compiler and platform, instead of assuming a fixed size.
4. Example
#include <iostream>
using namespace std;
int main() {
int quantity = 25;
float price = 19.99f;
double preciseValue = 3.14159265;
char grade = 'B';
bool inStock = true;
cout << "Quantity: " << quantity << " (size: " << sizeof(quantity) << " bytes)" << endl;
cout << "Price: " << price << endl;
cout << "Precise value: " << preciseValue << endl;
cout << "Grade: " << grade << endl;
cout << "In stock: " << inStock << endl;
return 0;
}5. Output
Quantity: 25 (size: 4 bytes)
Price: 19.99
Precise value: 3.14159
Grade: B
In stock: 16. Key Takeaways
- A data type defines what kind of value a variable can hold and how much memory it uses.
- The core fundamental types are int, float, double, char, bool, and void.
- Exact type sizes are implementation-defined — use
sizeof()to check them on your platform. - C++ also supports derived types (arrays, pointers) and user-defined types (class, struct, enum).
Practice what you learned
1. What does a data type specify for a variable?
2. Which data type is used to store true/false values in C++?
3. Which operator can you use to check the exact size of a data type on your system?
4. Which of these is generally more precise: float or double?
5. What is void commonly used for?
Was this page helpful?
You May Also Like
Type Modifiers in C++
Learn what type modifiers are in C++ — signed, unsigned, short, and long — and how they change size and range.
Variables in C++
Learn what are variables in C++, types of variables, declaration, initialization and examples with programs.
Literals in C++
Learn what literals are in C++ — integer, floating-point, character, string, and boolean literals — with syntax and examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics