Previous: Integer Range Overflow, Up: Integer Properties [Contents][Index]

These macros yield 1 if the corresponding C operators might not yield numerically correct answers due to arithmetic overflow of an integer type. They work correctly on all known practical hosts, and do not rely on undefined behavior due to signed arithmetic overflow. They expand to integer constant expressions if their arguments are. They are easier to use than the integer range overflow macros (see Integer Range Overflow).

Example usage:

#include <intprops.h> void print_product (long int a, long int b) { if (INT_MULTIPLY_OVERFLOW (a, b)) printf ("multiply would overflow"); else printf ("product is %ld", a * b); }

These macros have the following restrictions:

- Their arguments must be integer expressions.
- They may evaluate their arguments zero or multiple times, so the arguments should not have side effects.

These macros are tuned for their last argument being a constant.

`INT_ADD_OVERFLOW (`

`a`,`b`)-
Yield 1 if

would overflow. See above for restrictions.`a`+`b` `INT_SUBTRACT_OVERFLOW (`

`a`,`b`)-
Yield 1 if

would overflow. See above for restrictions.`a`-`b` `INT_NEGATE_OVERFLOW (`

`a`)-
Yields 1 if

`-`

would overflow. See above for restrictions.`a` `INT_MULTIPLY_OVERFLOW (`

`a`,`b`)-
Yield 1 if

would overflow. See above for restrictions.`a`*`b` `INT_DIVIDE_OVERFLOW (`

`a`,`b`)-
Yields 1 if

would overflow. See above for restrictions. Division overflow can happen on two’s complement hosts when dividing the most negative integer by -1. This macro does not check for division by zero.`a`/`b` `INT_REMAINDER_OVERFLOW (`

`a`,`b`)-
Yield 1 if

would overflow. See above for restrictions. Remainder overflow can happen on two’s complement hosts when dividing the most negative integer by -1; although the mathematical result is always 0, in practice some implementations trap, so this counts as an overflow. This macro does not check for division by zero.`a`%`b` `INT_LEFT_SHIFT_OVERFLOW (`

`a`,`b`)-
Yield 1 if

would overflow. See above for restrictions. The C standard says that behavior is undefined for shifts unless 0≤`a`<<`b``b`<`w`where`w`is`a`’s word width, and that when`a`is negative then

has undefined behavior and`a`<<`b`

has implementation-defined behavior, but this macro does not check these other restrictions.`a`>>`b`

Previous: Integer Range Overflow, Up: Integer Properties [Contents][Index]