Constant overflow

Dave Decot decot at hpisod2.HP.COM
Mon Mar 7 19:24:49 AEST 1988


> Try this one on your favorite C compiler.  So far, every one I've tried
> (including lint) has had no complaint:
> 
> 	long i = 9876543210L;
> 
> (The problem, of course, is that that number doesn't fit in 32 bits...)

My compiler and lint don't complain either.

I presume this phenomenon is widespread because the algorithm used for
lex'ing such constants is almost always something like "keep eating
digits and multiplying by 10 (or 8 or 16 or 2) until no more digits".
Optionally, this may include "...or until too many digits have been
processed", but that doesn't help this particular case, since there
aren't too many digits, just too much value.

It's hard to do this right unless you do some clever comparison
just before multiplying by 10 (or 8 or 16 or 2).

Dave Decot
hpda!decot



More information about the Comp.lang.c mailing list