toupper -- a lesson for programmers

Geoffrey Collyer geoff at utcsstat.UUCP
Sun Nov 13 14:36:09 AEST 1983


Martin Minow recently claimed

	The behavior of toupper() and tolower() varies across the many
	implementations of the C library.  The following strategies
	are known to work:
	
	1.	if (isupper(c))
		    c = tolower(c);
	
	2.	#ifdef	tolower
		#undef	tolower
		#define	tolower(c) (whatever you feel is right)

*Neither* of these is correct.  The first strategy should read

	#include <ctype.h>
	if (isascii(c) && isupper(c))
		c = tolower(c);

The second needs an #endif after #undef tolower.

I don't mean to pick nits, but far too much code exists right now
that doesn't use isascii to check that arguments to the other <ctype.h>
functions are in range.

Geoff Collyer, U. of Toronto



More information about the Comp.lang.c mailing list