Types and Sizes: Integers, Floating Point and Overflow You Can Check
Learn the fundamental C++ types, ask the compiler for their sizes and limits, see unsigned wrap-around, detect overflow safely with a compiler builtin and meet floating point rounding.
In this lesson you will
- Use the main fundamental types
- Ask for sizes and limits
- See unsigned wrap-around and detect overflow
- Understand floating point rounding
About 28 minutes
The counter that came back to zero
Elena counts the number of readings from her station in a variable declared as a small unsigned type, and after a long run the count suddenly starts again from zero. Every type in C++ has a fixed size and range, and calculations that go past the range behave in a way that depends on the type. Learning the types is learning what your numbers can and cannot hold.
| Type | Typical size on this platform | Notes |
|---|---|---|
| bool | 1 byte | true or false |
| char | 1 byte | A character or a small integer |
| short, int | 2 and 4 bytes | Whole numbers; int is the usual choice |
| long long | 8 bytes | Large whole numbers |
| unsigned versions | Same size | Only zero and positive values, wrap around on overflow |
| float, double | 4 and 8 bytes | Decimals; double has about 15 digits |
| std::size_t | 8 bytes on 64-bit | The type of sizes and indexes |
The sizes are not fixed by the standard, only minimums, so the program asks the compiler with sizeof, and the header provides std::numeric_limits for the ranges. Two rules matter. Unsigned integers wrap around: the standard says that an overflow simply continues modulo 2 to the power of the number of bits. Signed integer overflow is undefined behaviour, which means that the program has no defined meaning and the compiler may assume it never happens. For that reason the program below does not overflow a signed int at all, and shows the safe tools instead: an unsigned wrap, and the GCC builtin __builtin_add_overflow, which reports whether an addition overflowed without invoking undefined behaviour.
#include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
int main() {
std::cout << "sizeof: char " << sizeof(char) << ", short " << sizeof(short) << ", int " << sizeof(int)
<< ", long long " << sizeof(long long) << ", double " << sizeof(double) << ", size_t " << sizeof(std::size_t) << "\n";
std::cout << "int range: " << std::numeric_limits<int>::min() << " to " << std::numeric_limits<int>::max() << "\n";
std::cout << "unsigned char max: " << static_cast<int>(std::numeric_limits<unsigned char>::max()) << "\n";
unsigned char count = 250;
for (int i = 0; i < 10; ++i) ++count;
std::cout << "unsigned char 250 + 10 wraps to " << static_cast<int>(count) << "\n";
int a = std::numeric_limits<int>::max(), result = 0;
bool overflow = __builtin_add_overflow(a, 1, &result);
std::cout << "INT_MAX + 1 overflows: " << std::boolalpha << overflow << "\n";
long long big = static_cast<long long>(a) + 1;
std::cout << "as long long: " << big << "\n";
double d = 0.1 + 0.2;
std::cout.precision(17);
std::cout << "0.1 + 0.2 = " << d << ", equal to 0.3? " << (d == 0.3) << "\n";
std::cout << "within 1e-9 of 0.3? " << (std::abs(d - 0.3) < 1e-9) << "\n";
std::cout << "7 / 2 = " << 7 / 2 << ", 7 / 2.0 = " << 7 / 2.0 << ", -7 % 3 = " << -7 % 3 << "\n";
}
sizeof: char 1, short 2, int 4, long long 8, double 8, size_t 8
int range: -2147483648 to 2147483647
unsigned char max: 255
unsigned char 250 + 10 wraps to 4
INT_MAX + 1 overflows: true
as long long: 2147483648
0.1 + 0.2 = 0.30000000000000004, equal to 0.3? false
within 1e-9 of 0.3? true
7 / 2 = 3, 7 / 2.0 = 3.5, -7 % 3 = -1
On this platform the sizes are char 1, short 2, int 4, long long 8, double 8 and size_t 8 bytes, and the int range runs from -2,147,483,648 to 2,147,483,647. The sizes can differ on other systems, which is exactly why the program asks. An unsigned char holds 0 to 255, so adding ten to 250 wrapped around to 4, which is what the elapsed counter in Elena’s station did. The builtin reported true for INT_MAX + 1 without any undefined behaviour, and a wider type, long long, held the true value, 2,147,483,648.
Floating point shows the usual picture: 0.1 + 0.2 printed as 0.30000000000000004 with 17 digits of precision and was not equal to 0.3, though it was within a tiny tolerance, which is how doubles should be compared. The last line shows integer division, 7 / 2 giving 3 because both operands are integers, 7 / 2.0 giving 3.5, and -7 % 3 giving -1, since in C++ the remainder takes the sign of the first operand and division truncates towards zero.
Check by hand: 250 + 10 = 260, and 260 mod 256 is 4, the value that the wrap gave. 2 to the power 31 is 2,147,483,648, so the largest int is one less, 2,147,483,647. Python’s own arithmetic reproduces the same numbers, and its float addition gives the same 0.30000000000000004, since both use IEEE 754 doubles.
The compiler may assume it never happens and optimise accordingly, which can make code behave strangely. Use wider types, unsigned arithmetic where wrapping is meant, or checked builtins.
Use sizeof and std::numeric_limits instead of remembering sizes, and use fixed-width types such as std::int32_t from when the size matters.
Every program in this lesson was compiled with GCC 13 using -std=c++20 -Wall -Wextra and run while the course was written. C++ has a new standard every three years and compilers differ in what they support, so check your compiler’s documentation and cppreference.com for the current state. The sizes shown are for the course platform, a 64-bit Linux system.
Practice
Print the range of unsigned int, long long and float using numeric_limits. Find out what happens when you add one to the largest unsigned int, and detect an overflow in multiplication with __builtin_mul_overflow. Compare two doubles for equality with a tolerance.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does sizeof return?
- The number of items
- The speed
- The size of a type or object in bytes
- The address
It is evaluated at compile time.
What happens when an unsigned value overflows?
- It is undefined behaviour
- It wraps around modulo a power of two
- The program stops
- It becomes negative
250 + 10 became 4 for unsigned char.
Why is signed overflow dangerous?
- It is slow
- It is undefined behaviour
- It always crashes
- It prints a warning only
The compiler may assume it cannot happen.
How should doubles be compared?
- With a small tolerance
- With ==
- With sizeof
- They cannot be compared
0.1 + 0.2 was not equal to 0.3.
What is 7 / 2 for two ints?
- 3
- 3.5
- 4
- 2
Integer division truncates.
Summary
- Fundamental types have platform-dependent sizes, so ask with sizeof and numeric_limits.
- Unsigned wrap-around is defined; signed overflow is undefined behaviour.
- __builtin_add_overflow detects overflow safely.
- Compare doubles with a tolerance.
Questions
What is the difference between int and int32_t?
int has a platform-dependent size, usually 32 bits. std::int32_t is exactly 32 bits wherever it exists, and is declared in <cstdint>.
Why use size_t for sizes and indexes?
It is the type that sizeof and container sizes use, so it fits any size that the platform can hold.
What is a narrowing conversion?
Converting to a type that cannot hold every value, such as double to int. Brace initialisation, as in int x{3.5}, makes the compiler reject it.
