towards a faster isdigit()

Paul Eggert eggert at twinsun.com
Thu May 9 10:25:29 AEST 1991


ark at alice.att.com (Andrew Koenig) writes:

>> 	#define isdigit(c) ((unsigned)((c)-'0') < 10)
>... can you find a character set that meets the ANSI C constraints (in
>particular, the one that says that all digits are contiguous in the
>character set) for which this macro will not work?

Yes: any host where char is the same as int, and where '0' != 0.
The subtraction might overflow.

The following slightly revised isdigit() works around this problem
because unsigned arithmetic cannot overflow:

	#define isdigit(c) ((unsigned)(c) - '0'  <  10)

but frankly I worry that some old buggy compilers will generate the
wrong code for this ``more portable'' version.



More information about the Comp.lang.c mailing list