getch() and getche() in MSC 4.0
Richard A. O'Keefe
ok at quintus.uucp
Sat Oct 22 13:32:29 AEST 1988
In article <7594 at bloom-beacon.MIT.EDU> scs at adam.pika.mit.edu (Steve Summit) writes:
>This is a snide, whiney "I told you so" to the efficiency addicts
>and macro panderers out there.
>
> #define _toupper(c) ((c) - ('a' - 'A'))
> #define toupper(c) (islower(c) ? _toupper(c) : (c))
>
>If it is desirable for toupper to work correctly on characters
>that are nonalphabetic or already upper-case (I believe this
>property is called "idempotence," and as I said, it is a laudable
>goal), then the macro implementation has to be sacrificed, and
>toupper() made a proper function.
This conclusion does not follow. *THAT* version of toupper() has to
go, but you can still usefully use a macro.
extern char _utab[];
#define toupper(c) _utab[(c) & 255]
Merits: (1) single evaluation
(2) usually faster than a function call
(3) works nicely with EBCDIC or ISO 8859, not just ASCII
This is a good way of turning any function-from-characters into a macro:
compute all the function values when your program starts and store them
in an array. (Look at the is<class>() macros in /usr/include/ctype.h .)
More information about the Comp.lang.c
mailing list