Numeric Limits and Overflow Behaviour
Query std::numeric_limits and understand signed and unsigned overflow rules.
Numeric Limits and Overflow Behaviour is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Why Limits Matter
Every numeric type has a maximum and minimum representable value. Exceeding them can silently produce wrong results — or undefined behavior.
std::numeric_limits
Query the limits of any numeric type with the <limits> header. Compile-time constants — no runtime cost.
#include <limits>
std::cout << std::numeric_limits<int>::max() << "\n";
std::cout << std::numeric_limits<int>::min() << "\n";
std::cout << std::numeric_limits<double>::epsilon();Common Queries
The most useful members:
max()— largest valuemin()— smallest (positive) valuelowest()— most negativeepsilon()— smallest meaningful difference (floats)digits10— guaranteed decimal digits
Signed Integer Overflow
Signed integer overflow is undefined behavior. The compiler may assume it never happens, leading to surprising optimizations.
int x = std::numeric_limits<int>::max();
x += 1; // UB — anything can happenUnsigned Integer Wrap
Unsigned overflow is defined — values wrap around modulo 2^N. This is sometimes useful but often a footgun.
unsigned int u = 0;
u -= 1; // wraps to 4,294,967,295 (UINT_MAX)Float Overflow and Underflow
Floats overflow to inf instead of crashing. Underflow goes to subnormal numbers or 0.
double x = 1e308;
x *= 100;
std::cout << x; // infDetecting Overflow Safely
For signed integers, check before the operation. Compilers provide built-in checked-arithmetic helpers like __builtin_add_overflow on GCC/Clang.
if (a > std::numeric_limits<int>::max() - b) {
// overflow would happen
}
int sum = a + b;C++20 std::ssize and std::cmp_less
C++20 adds safe integer comparison helpers in <utility>: std::cmp_equal, cmp_less, cmp_greater. They handle signed/unsigned mixing correctly.
Implicit Conversions Are Sneaky
Mixing signed and unsigned in a comparison silently converts the signed operand to unsigned — a common source of subtle bugs.
int a = -1;
unsigned int b = 1;
if (a < b) std::cout << "yes";
else std::cout << "no"; // prints "no"!Compile-Time Checks
Use static_assert to enforce size assumptions when writing portable code.
static_assert(sizeof(int) >= 4, "Need at least 32-bit int");Sanitizers Help in Testing
Build with -fsanitize=undefined to catch signed overflow at runtime during testing.
g++ -fsanitize=undefined -O1 main.cppQuick Check
What happens when a signed integer addition overflows in C++?
Recap
Use std::numeric_limits to query type bounds. Signed overflow is undefined; unsigned wraps. Mix signed and unsigned at your peril. Sanitizers and C++20 safe comparisons help guard your code.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Numeric Limits and Overflow Behaviour” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Numeric Limits and Overflow Behaviour”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Numeric Limits and Overflow Behaviour”?
Query std::numeric_limits and understand signed and unsigned overflow rules. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Numeric Limits and Overflow Behaviour” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Integer vs Floating Point Types
- The cmath Header: pow sqrt abs round
- Random Numbers with the random Header
- Numeric Limits and Overflow Behaviour