macros with parameters in MSC 5.0

Doug Gwyn gwyn at brl-smoke.ARPA
Fri Feb 19 08:10:56 AEST 1988


In article <11879 at brl-adm.ARPA> jbaker at ee.UCLA.EDU (Joe Baker) writes:
>#define ctl(c) ('c'&037)

Yes, whoever did this was deliberately violating the K&R rules, because
they knew that their C preprocessor (probably based on Reiser's) would
allow it.  This particular macro and Berkeley's _IOR etc. ioctl macros
are the most common cases of this nonportable construct.  Neither was
necessary, since the desired effect could have been achieved portably.

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

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)
which would cause
	case ctl(G):
to expand to
	case ("G"[0]&037):
which is equivalent to
	case ('G'&037):
Personally, I prefer the other way I described, because it also works
with pre-ANSI C preprocessors.



More information about the Comp.lang.c mailing list