Trouble with curses

Steve Summit scs at adam.mit.edu
Thu Nov 8 10:33:56 AEST 1990


In article <1990Nov7.194154.6071 at ssd.kodak.com> weimer at 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 at adam.mit.edu



More information about the Comp.lang.c mailing list