In C++, data types are divided into fundamental (also known as primitive) and compound types. This chapter focuses on the fundamental data types, their sizes, ranges, and usage guidelines.
Category | Type | Minimum Size | Typical Size | Note |
---|---|---|---|---|
Boolean | bool | 1 byte | 1 byte | |
Character | char | 1 byte | 1 byte | Always exactly 1 byte |
wchar_t | 1 byte | 2 or 4 bytes | ||
char8_t | 1 byte | 1 byte | ||
char16_t | 2 bytes | 2 bytes | ||
char32_t | 4 bytes | 4 bytes | ||
Integer | short | 2 bytes | 2 bytes | |
int | 2 bytes | 4 bytes | ||
long | 4 bytes | 4 or 8 bytes | ||
long long | 8 bytes | 8 bytes | ||
Floating Point | float | 4 bytes | 4 bytes | |
double | 8 bytes | 8 bytes | ||
long double | 8 bytes | 8, 12, or 16 bytes | ||
Pointer | std::nullptr_t | 4 bytes | 4 or 8 bytes |
int a;
short a;
signed short a;
short int a;
long a;
long long a;
An n-bit signed variable has a range of -(2^{n-1})
to 2^{n-1} - 1
:
Bits | Range |
---|---|
8 | -128 to 127 |
16 | -32,768 to 32,767 |
32 | -2,147,483,648 to 2,147,483,647 |
64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned short us;
unsigned int ui;
unsigned long ul;
unsigned long long ull;
An n-bit unsigned variable holds values from 0 to (2^n) - 1
:
Bits | Range |
---|---|
8 | 0 to 255 |
16 | 0 to 65,535 |
32 | 0 to 4,294,967,295 |
64 | 0 to 18,446,744,073,709,551,615 |
Larger numbers are stored as % 256:
257 % 256 = 1 (stored)
<cstdint>
header)Name | Type | Range | Notes |
---|---|---|---|
std::int8_t | 1 byte signed | -128 to 127 | Treated like a signed char on many systems. |
std::uint8_t | 1 byte unsigned | 0 to 255 | Treated like an unsigned char on many systems. |
std::int16_t | 2 byte signed | -32,768 to 32,767 | |
std::uint16_t | 2 byte unsigned | 0 to 65,535 | |
std::int32_t | 4 byte signed | -2,147,483,648 to 2,147,483,647 | |
std::uint32_t | 4 byte unsigned | 0 to 4,294,967,295 | |
std::int64_t | 8 byte signed | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | |
std::uint64_t | 8 byte unsigned | 0 to 18,446,744,073,709,551,615 |
int
when size doesn't matter.std::int#_t
for a guaranteed range (fixed-width integers).std::uint#_t
for bit manipulation or well-defined wrap-around behavior.short
and long
integers; use fixed-width types instead.std::size_t
The size of std::size_t
imposes a strict upper limit on an object's size.
#include <cstddef>
int x { 5 };
std::size_t s { sizeof(x) }; // sizeof returns a value of type std::size_t
std::cout << sizeof(std::size_t) << '\n';
double b { 5.0 }; // 5.0 is a floating point literal (double by default)
float c { 5.0f }; // 5.0 is a floating point literal (float type)
Size | Range | Significant Digits |
---|---|---|
4 bytes | ±1.18 x 10^-38 to ±3.4 x 10^38 and 0.0 | 6-9 digits, typically 7 |
8 bytes | ±2.23 x 10^-308 to ±1.80 x 10^308 and 0.0 | 15-18 digits, typically 16 |
80-bits | ±3.36 x 10^-4932 to ±1.18 x 10^4932 and 0.0 | 18-21 digits |
16 bytes | ±3.36 x 10^-4932 to ±1.18 x 10^4932 and 0.0 | 33-36 digits |
#include <iomanip> // for output manipulator std::setprecision()
std::cout << std::setprecision(17); // show 17 digits of precision
std::cout << 3.33333333333333333333333333333333333333f << '\n'; // float
std::cout << 3.33333333333333333333333333333333333333 << '\n'; // double
double posinf { 5.0 / zero }; // positive infinity
double nan { zero / zero }; // not a number (mathematically invalid)
bool b;
std::cin >> std::boolalpha; // Allow the user to input 'true' or 'false'
std::cin >> b;
std::cout >> std::boolalpha; // Output bool values as `true` or `false`
std::cout << "You entered: " << b << '\n';
bool bNo { 2 }; // error: narrowing conversions disallowed
bool b1 = 4; // copy initialization allows implicit conversion from int to bool
// 0 -> false, everything else -> true
static_cast<new_type>(expression)
static_cast<int>(5.5); // no warnings
static_cast<int>(ch); // char to int
The integer
stored by a char
variable is interpreted as an ASCII character
. ASCII table
char ch2 { 'a' }; // initialize with code point for 'a' (stored as integer 97) (preferred)
char ch1 { 97 }; // initialize with integer 97 ('a') (not preferred)
Avoid multicharacter literals (e.g., '56'
).