[comp.lang.c] macros with parameters

jbaker@ee.UCLA.EDU (Joe Baker) (02/20/88)

In article <11879@brl-adm.ARPA> I write:
>#define ctl(c) ('c'&037)
(doesn't work in MSC 5.0, and seems to violate K&R)

Doug Gwyn (and several others) responded:

>The correct way to have done this would have been:
>	#define ctl(c) ((c)&037)
>and include the '' in its invocation:
>	case ctl('G'):

which works great. Doug then wrote

>With ANSI C, or any C preprocessor that includes the stringizing
>feature, you can make the original usage work by
>	#define ctl(c) (#c[0]&037)
...
>Personally, I prefer the other way I described, because it also works
>with pre-ANSI C preprocessors.

Actually, this second approach doesn't work with MSC 5.0
(it was my first attempt at a work-around.)  When invoked in a case
statement, the compiler complains that the case expression is not
constant.

Thanks to everyone for all the help.

- Joe Baker, Dept. of Electrical Engineering
6731 Boelter Hall, UCLA, L.A., CA 90024 (213) 825-7079, 825-2327
ARPA: jbaker@ee.ucla.edu UUCP: {ihnp4|randvax|ucbvax}!ucla-cs!uclaee!jbaker