In this lesson, you will learn.
Here’s a pictorial representation of each data types in C++.

#include<iostream>
int main() {
// Integer type
int age = 25;
std::cout << "Age: " << age << std::endl;
// Floating-point type
float weight = 70.5;
std::cout << "Weight: " << weight << " kg" << std::endl;
// Double-precision floating-point type
double height = 175.25;
std::cout << "Height: " << height << " cm" << std::endl;
// Character type
char initial = 'A';
std::cout << "Initial: " << initial << std::endl;
// Boolean type
bool isStudent = true;
std::cout << std::boolalpha; // To print bool values as true/false instead of 1/0
std::cout << "Is student: " << isStudent << std::endl;
// Demonstrating arithmetic operations with primitive types
int num1 = 10, num2 = 20;
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
double quotient = static_cast(num1) / num2;
std::cout << "\nArithmetic Operations:" << std::endl;
std::cout << num1 << " + " << num2 << " = " << sum << std::endl;
std::cout << num1 << " - " << num2 << " = " << difference << std::endl;
std::cout << num1 << " * " << num2 << " = " << product << std::endl;
std::cout << num1 << " / " << num2 << " = " << quotient << std::endl;
// Demonstrating type casting
double preciseResult = 10.0 / 3.0;
int roundedResult = static_cast(preciseResult);
std::cout << "\nType Casting:" << std::endl;
std::cout << "Precise result: " << preciseResult << std::endl;
std::cout << "Rounded result: " << roundedResult << std::endl;
return 0;
}
In C++, static_cast is a type of casting operator used for performing explicit type conversions. It is used to convert one type to another in a way that is checked at compile time.
static_cast<new_type>(expression)
int main() {
int a = 10;
double b = static_cast(a); // Convert int to double
std::cout << "Value of b: " << b << std::endl; // Output: 10.0
return 0;
}
Here’s a summary table of the various data types in C++ including their size, range, example, and default values
| Data Type | Size (bytes) | Range | Example | Default Value |
|---|---|---|---|---|
| Basic Data Types | ||||
int | 4 | -2,147,483,648 to 2,147,483,647 | int x = 42; | 0 |
short int | 2 | -32,768 to 32,767 | short int y = 10; | 0 |
long int | 4 or 8 | -2,147,483,648 to 2,147,483,647 or larger depending on platform | long int z = 100000L; | 0 |
long long int | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | long long int a = 123456789LL; | 0 |
unsigned int | 4 | 0 to 4,294,967,295 | unsigned int b = 50U; | 0 |
float | 4 | 1.2E-38 to 3.4E+38 | float pi = 3.14f; | 0.0f |
double | 8 | 2.3E-308 to 1.7E+308 | double e = 2.71828; | 0.0 |
long double | 8 or 16 | 3.4E-4932 to 1.1E+4932 | long double ld = 3.141592653589793238L; | 0.0L |
char | 1 | -128 to 127 | char ch = 'A'; | ‘\0’ |
bool | 1 | true or false | bool flag = true; | false |
| Derived Data Types | ||||
int[] (Array) | varies | depends on size | int arr[3] = {1, 2, 3}; | – |
int* (Pointer) | 4 or 8 | address space dependent | int* ptr = &x; | nullptr |
int& (Reference) | same as the referenced type | same as the referenced type | int& ref = x; | N/A |
| User-Defined Data Types | ||||
struct | varies | depends on members | struct Person { string name; int age; }; | N/A |
union | varies | depends on the largest member | union Data { int i; float f; }; | N/A |
enum | varies | typically 4 bytes, range depends on values | enum Color { RED, GREEN, BLUE }; | N/A |
void is a special type that indicates the absence of a value. It is used in different contexts to specify that a function does not return a value or that a pointer is of an unspecified type.
The amount of memory occupied by the integer types is system dependent. On a 32-bit system such as Windows, an int occupies 4 bytes (which is 32 bits) of memory.
The basic datatypes may have modifiers preceding them to alter the meaning of base type. The following are the modifiers in C++.

| S.N | Specifier | Same As |
| 1 | Signed | signed int |
| 2 | Unsigned | unsigned int |
| 3 | long | long int |
| 4 | short | short int |
The type long always occupies four bytes, which is the same as type int on 32-bit Windows systems. It can also be written as long int; this means the same as long.
Answer: If your program may need to run on a 16-bit system such as MS-DOS, or on older versions of Windows, specifying type long will guarantee a four-bit integer type. In 16-bit systems, type int has the same range as type short(i.e. bytes).
If you want to create a constant of type long, use the letter L following the numerical value, as in
long longvar = 7678L; // assigns long constant 7678 to longvar.
wchar_t in C++ is implementation-defined, meaning it can vary depending on the compiler, operating system, and platform. It may of 2 or 4 byte as per the size of 16 or 32 OS.Here’s a simple C++ program that displays the size (in bytes) of all primitive data types using the sizeof operator:
#include<iostream>
using namespace std;
int main() {
cout << "Size of primitive data types in C++ (in bytes):\n";
cout << "-----------------------------------------------\n";
cout << "char: " << sizeof(char) << " byte(s)\n";
cout << "bool: " << sizeof(bool) << " byte(s)\n";
cout << "short: " << sizeof(short) << " byte(s)\n";
cout << "int: " << sizeof(int) << " byte(s)\n";
cout << "long: " << sizeof(long) << " byte(s)\n";
cout << "long long: " << sizeof(long long) << " byte(s)\n";
cout << "float: " << sizeof(float) << " byte(s)\n";
cout << "double: " << sizeof(double) << " byte(s)\n";
cout << "long double: " << sizeof(long double) << " byte(s)\n";
wchar_t w;
cout << "wchar_t: " << sizeof(wchar_t) << " byte(s)\n";
return 0;
}
Size of primitive data types in C++ (in bytes):
-----------------------------------------------
char: 1 byte(s)
bool: 1 byte(s)
short: 2 byte(s)
int: 4 byte(s)
long: 4 byte(s)
long long: 8 byte(s)
float: 4 byte(s)
double: 8 byte(s)
long double: 16 byte(s)
wchar_t: 4 byte(s)
Note: The size may vary between systems (e.g., 32-bit vs 64-bit). Always test on your system.
#include<iostream>
#include<climits> // For INT_MIN, INT_MAX, etc.
#include<cfloat> // For FLT_MIN, FLT_MAX, etc>
using namespace std;
int main() {
cout << "Range of Primitive Data Types in C++:\n";
cout << "--------------------------------------\n";
// Integer types
cout << "char: " << CHAR_MIN << " to " << CHAR_MAX << endl;
cout << "unsigned char: 0 to " << UCHAR_MAX << endl;
cout << "short: " << SHRT_MIN << " to " << SHRT_MAX << endl;
cout << "unsigned short: 0 to " << USHRT_MAX << endl;
cout << "int: " << INT_MIN << " to " << INT_MAX << endl;
cout << "unsigned int: 0 to " << UINT_MAX << endl;
cout << "long: " << LONG_MIN << " to " << LONG_MAX << endl;
cout << "unsigned long: 0 to " << ULONG_MAX << endl;
cout << "long long: " << LLONG_MIN << " to " << LLONG_MAX << endl;
cout << "unsigned long long: 0 to " << ULLONG_MAX << endl;
cout << endl;
// Floating-point types
cout << "float: " << FLT_MIN << " to " << FLT_MAX << endl;
cout << "double: " << DBL_MIN << " to " << DBL_MAX << endl;
cout << "long double: " << LDBL_MIN << " to " << LDBL_MAX << endl;
cout << endl;
// Boolean
cout << "bool: " << false << " (false) to " << true << " (true)" << endl;
return 0;
}
Range of Primitive Data Types in C++:
--------------------------------------
char: -128 to 127
unsigned char: 0 to 255
short: -32768 to 32767
unsigned short: 0 to 65535
int: -2147483648 to 2147483647
unsigned int: 0 to 4294967295
long: -2147483648 to 2147483647
unsigned long: 0 to 4294967295
long long: -9223372036854775808 to 9223372036854775807
unsigned long long: 0 to 18446744073709551615
float: 1.17549e-38 to 3.40282e+38
double: 2.22507e-308 to 1.79769e+308
long double: 3.3621e-4932 to 1.18973e+4932
bool: 0 (false) to 1 (true)
A great learning platform
You must be logged in to submit a review.