Is there a good example of how toupper() works?

Mark Brader msb at sq.sq.com
Sun Oct 21 16:09:52 AEST 1990


Not yet pointed out in all this discussion is that just because
you retrieve a value through a pointer of type char *, it isn't
necessarily a permissible argument of EITHER islower() or toupper().

In early implementations, an argument of islower() or toupper()
has to be in the range 0 to 127, which isascii() checks.  In ANSI
implementations, isascii() is allowed to not exist, but the
argument of islower() or toupper() can validly go as high as
MAX_UCHAR, so you only need to ensure the char is nonnegative.

This, then, should be a solution:

	#ifdef __STDC__			/* ANSI C */
	# if (MAX_CHAR < MAX_UCHAR)	/* chars are signed */
	#   define TOUPP(c) ((c) < 0? (c): toupper((c)))
	# else
	#   define TOUPP(c) toupper((c))
	# endif
	#else
	# define TOUPP(c) ((isascii((c)) && islower((c))? toupper((c)): ((c)))
	#endif

	for (p = duh; *p != '\0'; ++p)
		*p = TOUPP(*p);

-- 
Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb at sq.com
#define	MSB(type)	(~(((unsigned type)-1)>>1))

This article is in the public domain.



More information about the Comp.lang.c mailing list