Path: utzoo!attcan!uunet!wuarchive!usc!elroy.jpl.nasa.gov!zardoz.cpd.com!dhw68k!felix!asylvain From: asylvain@felix.UUCP (Alvin "the Chipmunk" Sylvain) Newsgroups: comp.lang.c Subject: Re: Is there a good example of how toupper() works? Keywords: converting upper/lower case macros Message-ID: <152638@felix.UUCP> Date: 25 Oct 90 21:42:09 GMT References: <1990Oct17.170914.683@wpi.WPI.EDU> <11021@hubcap.clemson.edu> <152580@felix.UUCP> Sender: daemon@felix.UUCP Reply-To: asylvain@felix.UUCP (Alvin "the Chipmunk" Sylvain) Organization: FileNet Corp., Costa Mesa, CA Lines: 36 Ye gadz, recursive follow-ups! In article <152580@felix.UUCP> asylvain@felix.UUCP, I wrote: > In article <11021@hubcap.clemson.edu> svissag@hubcap.clemson.edu > (Steve L Vissage II) writes: > > From article <1990Oct17.170914.683@wpi.WPI.EDU>, by profesor@wpi.WPI.EDU > > (Matthew E Cross): > > > Nope, won't work - the return value of 'toupper' is undefined if the input > > > is not a lowercase character. > > > > So define your own toupper() macro. That's what I did. > > #define toupper(ch) ((ch<123 && ch>96) ? ch-32 : ch) > > Two points: first, your macro is *disgustingly unreadable* (and probably > incorrect ...haven't checked). This is better: > > #define toupper(ch) (((ch) >= 'a' && (ch) <= 'z') ? (ch) + 'A' - 'a' : (ch)) > #define tolower(ch) (((ch) >= 'A' && (ch) <= 'Z') ? (ch) + 'a' - 'A' : (ch)) Upon re-reading this, I've decided that it's not *much* better from a readability point of view. Try this: #define toupper(ch) (((ch) >= 'a' && (ch) <= 'z') \ ? (ch) + 'A' - 'a' \ : (ch)) #define tolower(ch) (((ch) >= 'A' && (ch) <= 'Z') \ ? (ch) + 'a' - 'A' \ : (ch)) Please note that spaces are deliberate. This is also what is known as a "dangerous" macro, in that if you pass it something like '*ch++', your results may not be what you expect. Therefore, following convention, it ought to be TOUPPER and TOLOWER as warning. I still maintain that you should forget the whole thing and use the library functions.