towards a faster isdigit()

Paul Eggert eggert at twinsun.com
Wed May 8 13:05:15 AEST 1991


The traditional implementation of isdigit() in <ctype.h> is typically
something like this:

	#define isdigit(c) ((_ctype_+1)[c] & 4)

which requires indexing through a global array followed by a masking
operation.  Why not use the following implementation instead?

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

This needs just a subtraction followed by a comparison.  It's faster on
all the systems I've tried it on, and is strictly conforming ANSI C.



More information about the Comp.lang.c mailing list