Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Operators and Expressions

Integer overflow in C

Reference for integer overflow in C: defined wrap-around for unsigned types, undefined behavior for signed ones, safe checks and checked arithmetic in C23.

Integer overflow happens when an arithmetic result does not fit in the type that has to hold it. C treats the two families of integers very differently. Unsigned arithmetic is defined to wrap around modulo 2^N, where N is the number of value bits. Signed arithmetic that overflows is undefined behavior: the program has no defined meaning from that point, and the compiler may assume it never happens.

That assumption is real. Optimizers delete overflow checks written as x + 1 < x for signed x, and loops that rely on wrap-around can be transformed into something different. Check before you compute, use unsigned types when you want wrap-around, or use the checked operations added in C23.

C
unsigned u = UINT_MAX;
u = u + 1;          /* defined: u is 0 */
int i = INT_MAX;
i = i + 1;          /* undefined behavior */

Try it

C Defined wrap-around and safe checks

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.

C Checked arithmetic with stdckdint.h (C23)

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

There is no operator for overflow; it is a property of arithmetic operators (+ - * << ++ -- and unary -) and conversions. Rules by type:

Case Result
Unsigned arithmetic does not fit Reduced modulo 2^N. Defined.
Signed arithmetic does not fit Undefined behavior.
Conversion of an out-of-range value to an unsigned type Reduced modulo 2^N. Defined.
Conversion of an out-of-range value to a signed type Implementation-defined (GCC wraps) or an implementation-defined signal.
Types narrower than int (char, short) Promoted to int first, so unsigned char arithmetic itself rarely overflows; the overflow, if any, happens at the conversion back.
INT_MIN / -1, INT_MIN % -1, -INT_MIN, abs(INT_MIN) Undefined behavior.
Left shift of a signed value whose result does not fit, or a shift count at or above the width Undefined behavior.

C23 adds <stdckdint.h> with ckd_add, ckd_sub and ckd_mul: bool ckd_add(R *result, A a, B b). They compute the mathematically exact result, store it in *result converted to the type of R (wrapped if it does not fit) and return true if the stored value is not the exact result. GCC 15.2 accepts them in our runner.

Errors / undefined behavior: signed overflow is undefined behavior even if the machine would wrap.

Usage notes

  • Check before, not after. if (a + b < a) is not a valid test for signed a, b: the overflow already happened. Use the INT_MAX - b form shown above or ckd_add.
  • Unsigned is not safer, just defined. size_t n = a - b; with a < b wraps to a huge number and often becomes a huge allocation or a runaway loop. Compare before subtracting.
  • Allocation sizes. malloc(count * size) can wrap around and allocate a small buffer. Use calloc(count, size) (which must check) or check the multiplication.
  • Mixed signed/unsigned comparisons convert the signed operand to unsigned: -1 < 1u is false. This is a related surprise; see type conversions.
  • Compiler flags. -fwrapv makes GCC and Clang treat signed overflow as wrap-around (non-portable); -ftrapv and -fsanitize=signed-integer-overflow catch it while testing. Neither is a substitute for correct code.
  • Floating-point does not overflow into undefined behavior: it produces infinity (IEEE 754), see floating-point comparison. Converting an out-of-range floating value to an integer is undefined.
  • Loop counters. A signed loop like for (int i = 0; i >= 0; i++) is an overflow bug waiting to be optimized into an endless loop.
  • Printing a wrapped value only shows the effect; it never shows signed overflow, because that is undefined and so is not demonstrated here.