prev up next   top/contents search

comp.lang.c FAQ list · Question 10.21

Q: I have an old macro

#define CTRL(c) ('c' & 037)
that doesn't seem to work any more.

A: The intended use of this macro is in code like

	tchars.t_eofc = CTRL(D);
which is expected to expand to
	tchars.t_eofc = ('D' & 037);
based on the assumption that the actual value of the parameter c will be substituted even inside the single quotes of a character constant. Preprocessing was never supposed to work this way; it was somewhat of an accident that a CTRL() macro like this ever worked.

ANSI C defines a new ``stringizing'' operator (see question 11.17), but there is no corresponding ``charizing'' operator.

The best solution to the problem is probably to move the single quotes from the definition to the invocation, by rewriting the macro as

	#define CTRL(c) ((c) & 037)
and invoking it as
(Doing so also makes the macro ``syntactic''; see question 10.2.)

It may also be possible to use the stringizing operator and some indirection:

	#define CTRL(c) (*#c & 037)
	#define CTRL(c) (#c[0] & 037)
but neither of these would work as well as the original, since they wouldn't be valid in case labels or as global variable initializers. (Global variable initializers and case labels require various flavors of constant expressions, and string literals and indirection aren't allowed.)

See also question 11.18.

References: ISO Sec. 6.8.3
H&S Secs. 7.11.2,7.11.3 pp. 226-7

prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North