Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!ucla-cs!twinsun!eggert From: eggert@twinsun.com (Paul Eggert) Newsgroups: comp.lang.c Subject: Re: towards a faster isdigit() Message-ID: <1991May9.002529.12044@twinsun.com> Date: 9 May 91 00:25:29 GMT References: <1991May8.030515.7004@twinsun.com> <20325@alice.att.com> Sender: usenet@twinsun.com Organization: Twin Sun, Inc Lines: 17 Nntp-Posting-Host: ata ark@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.