_tolower and _toupper macros

Bruce Worden bruce at seismo.gps.caltech.edu
Wed Jul 25 12:12:18 AEST 1990


Not to drag this out too much more, but:

In article <2891 at dftsrv.gsfc.nasa.gov> vander at nssdcb.gsfc.nasa.gov writes:
>its  amazing that _toupper and _tolower "misbehave" on the SUN's
>it seems like they do the masking without doing the checking

>from VAXC v3.0 ctype.h

>#define _toupper(c)	((c) >= 'a' && (c) <= 'z' ? (c) & 0x5F:(c))
>#define _tolower(c)	((c) >= 'A' && (c) <= 'Z' ? (c) | 0x20:(c))

>work good-to-go
 ^^^^^^^^^^^^^^^
Well, only if you don't mind evaluating (c) three times.  If (c) has
side effects, as with any macro, you may have problems.  To wit,

...
a = _tolower(getchar());
...

would produce a disaster.  That is why the versions above are 
preceded by the underscore, so that they will not be accidently used in 
place of the more robust toupper() and tolower() functions that you 
undoubtably have on your system.  

Once again: under SunOS 4.1 tolower() works as per the standard for either 
the ucb or sys V compiler, the sys V compiler also works "correctly" 
under 4.0.3 (and probably before).  

(Interestingly enough under 4.1 the in the Sys V and the ucb ctype.h 
_tolower() and _toupper() convert *without* checking, just the opposite of 
the example given above.  These macros should probably be avoided, unless 
maximum performance is desired (and the programmer is sure of what he is 
doing.))

Sorry about all of the Sun specific stuff, folks.

					Bruce
Disclaimer: I do not speak for Sun Microsystems nor do I even necessarily
like them all that much.



More information about the Comp.lang.c mailing list