Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!ucsd!ucbvax!bloom-beacon!daemon From: scs@adam.mit.edu (Steve Summit) Newsgroups: comp.lang.c Subject: Re: Trouble with curses Message-ID: <1990Nov7.233356.15324@athena.mit.edu> Date: 7 Nov 90 23:33:56 GMT References: <173826@<1990Oct27> <20900012@inmet> <1990Nov7.194154.6071@ssd.kodak.com> Sender: daemon@athena.mit.edu (Mr Background) Reply-To: scs@adam.mit.edu (Steve Summit) Organization: Thermal Technologies, Inc. Lines: 32 In article <1990Nov7.194154.6071@ssd.kodak.com> weimer@ssd.kodak.com (Gary Weimer) writes: >ANSI complient definition of tolower() does call islower() before >making the conversion. In this case, the code "expands" to: > switch(islower(getch()) ? tolower(getch()) : (getch())) { >Note that you are now always making two calls to getch() (as someone >has already pointed out is a possibility). Yes, and the earlier pointings-out were almost as misleading as this one. ANSI does specify (Section 4.3.2.1) that tolower() leaves non-upper-case letters alone, HOWEVER it also requires (Section 4.1.6) that "Any invocation of a library function that is implemented as a macro shall expand to code that evaluates each of its arguments exactly once..." (The rules in Section 4.1.6 apply unless they are explicitly retracted for a particular library routine, but Section 4.3.2 contains no such retraction for tolower or toupper.) We can conclude, then, that an ANSI- compliant tolower must be implemented as an actual function, or perhaps as a macro with a lookup table, but certainly NOT as #define tolower(c) (isupper(c) ? (c) + ('a' - 'A') : (c)) /* WRONG */ To repeat: switch(tolower(getchar())) is "safe" under an ANSI compiler. (Whether that's the best way to write it is another question.) The earlier correspondent was having to hit double characters due to some other problem, or due to an invalid tolower() implementation. Moving the getchar() out of the tolower() is not required, except to work around a buggy compiler/library, or perhaps to improve readability or error checking. Steve Summit scs@adam.mit.edu