C Reference ยท ctype.h, math.h and others
C stdckdint.h: checked integer arithmetic (C23)
Reference for the C23 header stdckdint.h: ckd_add, ckd_sub and ckd_mul detect integer overflow without undefined behavior, with examples for sizes and sums.
<stdckdint.h> provides three type-generic macros, ckd_add, ckd_sub and ckd_mul, that perform an arithmetic operation as if with infinite precision and tell you whether the result fits in the destination type. They return true when the true result does not fit, and in that case store the wrapped (modulo) value. Signed overflow with ordinary operators is undefined behavior; these macros give you a defined, portable way to detect it.
Unlike the hand-written checks in Integer overflow, the operands and the result may all have different types, and the macros handle mixed signedness correctly. GCC 15 accepts the header on the runner (the header comes with the compiler).
#include <stdckdint.h> int sum; if (ckd_add(&sum, a, b)) { /* a + b does not fit in an int */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
bool ckd_add(type1 *result, type2 a, type3 b); bool ckd_sub(type1 *result, type2 a, type3 b); bool ckd_mul(type1 *result, type2 a, type3 b);
| Name | Description |
|---|---|
result |
Pointer to the object that receives the (possibly wrapped) result. May be any integer type except bool, plain char or an enumerated type. |
a, b |
The operands: integer expressions of any integer type, with the same exclusions. |
Returns: true if the mathematically exact result cannot be represented in *result, false if it can.
Errors / undefined behavior: none for the arithmetic itself, because the operation never overflows in the usual sense. Passing a floating-point operand, a bool, or a pointer of the wrong kind is a constraint violation.
Usage notes
- The result is always written, even on overflow: it is the exact result reduced modulo 2^N for the destination type. That matches unsigned arithmetic and is the two's complement wrap for signed types. Do not use it unless the call returned
false(or you deliberately want wrapping). - The destination decides the answer.
200 + 100fits inintbut not inunsigned char, so the same operands give different flags for different result types. - Mixed signedness is handled exactly:
ckd_add(&u, -1, 0)reports overflow because-1is not representable in an unsigned type; ordinary-1 + 0uwould silently become a huge number. - Allocation sizes:
ckd_mul(&bytes, count, size)beforemallocprevents the classic integer-overflow heap bug. Compare withcalloc, which performs the same check internally. - Compilers implement them as builtins (
__builtin_add_overflowand friends), so the code is as cheap as the hand-written check, often a single flag test after the instruction. - They are macros. Arguments may be evaluated once; avoid side effects anyway, and do not take their address.
- Not for floating point. Use
fenv.hflags for overflow in floating-point math. - Older compilers: before C23 use the GCC/Clang builtins
__builtin_add_overflow(same argument order, result pointer last) or write explicit range checks like the one in limits. - Standard version: C23. The header is
<stdckdint.h>; there is no C++ equivalent in the standard library yet.