checking for overflow in C

Doug Gwyn gwyn at smoke.BRL.MIL
Wed May 10 01:11:38 AEST 1989


In article <13003 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:
>Let's take a concrete example.  How do you think one should code the
>implementation of the ANSI C function strtol(), which is required to detect
>integer overflow and return ERANGE in this case?

There's no requirement that strtol() be implemented in C, but assuming
that you're trying to do so, in the loop that multiplies the accumulator
by the base and adds in the next digit, before doing that operation test
to see if it would overflow.  It's not hard.



More information about the Comp.lang.c mailing list