isascii (was: Another pitfall. Signed chars and ctype.h)

Mark Brader msb at sq.sq.com
Mon Feb 12 15:33:24 AEST 1990


> >	isascii(*s) && isdigit(*s)
> 
> According to _Standard C_ ... there is no "isascii".  And "isdigit" etc.
> take an int in the set (EOF, 0..UCHAR_MAX) ...

> So, to write ANSI conformant C you must always say something like
> 	isdigit((unsigned char) *s)

If the code has to run on ANSI and non-ANSI C's, I'd prefer:

	#include <stdio.h>
	#include <ctype.h>

	#ifndef isascii		/* oh, must be ANSI C */
	#define isascii(x) (((x) >= 0 && (x) < UCHAR_MAX) || (x) == EOF))
	#endif

and then
	isascii(*s) && isdigit(*s)

The X3J11 people did not put isascii() in ANSI C because of the "ascii"
part of the name.  Correctly, they did not want to make any part of the
C Standard ASCII-dependent.  I suggested that isascii() be guaranteed
merely to have semantics similar to the above #define and the name kept
as a historical artifact, but they didn't buy it.

To keep this article short, I won't discuss making it work when the
argument has side-effects (as in isascii (*p++)).

-- 
Mark Brader		    At any rate, C++ != C.  Actually, the value of the
SoftQuad Inc., Toronto	    expression "C++ != C" is implementation-defined.
utzoo!sq!msb, msb at sq.com				-- Peter da Silva

This article is in the public domain.



More information about the Comp.lang.c mailing list