Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!usc!jarthur!nntp-server.caltech.edu!seismo.gps.caltech.edu!bruce From: bruce@seismo.gps.caltech.edu (Bruce Worden) Newsgroups: comp.lang.c Subject: Re: _tolower and _toupper macros Message-ID: <1990Jul25.021218.23142@laguna.ccsf.caltech.edu> Date: 25 Jul 90 02:12:18 GMT References: <2891@dftsrv.gsfc.nasa.gov> Sender: bruce@seismo.gps.caltech.edu (Bruce Worden) Organization: California Institute of Technology, CA Lines: 40 Not to drag this out too much more, but: In article <2891@dftsrv.gsfc.nasa.gov> vander@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.