|
|
In pre-ANSI C compilers, the following produced the string literal ``"x y!"'':
#define str(a) "a!" ++ str(x y)Thus the preprocessor searched inside string literals (and character constants) for characters that looked like macro parameters. ANSI C recognized the importance of this feature, but could not condone operations on parts of tokens. (In ANSI C, all invocations of the above macro produce the string literal ``"a!"''.) To achieve the old effect in ANSI C, we make use of the # macro substitution operator and the concatenation of string literals.
#define str(a) #a "!" str(x y)The above produces the two string literals ``"x y"'' and ``"!"'' which, after concatenation, produces the identical ``"x y!"''.
Unfortunately, there is no direct replacement for the analogous operation for character constants. The major use of this feature was similar to the following:
#define CNTL(ch) (037 & 'ch') ++ CNTL(L)which produced
(037 & 'L')which evaluates to the ASCII control-L character. The best solution is to change all uses of this macro (at least this can be done automatically) to:
#define CNTL(ch) (037 & (ch)) CNTL('L')which is arguably more readable and more useful, as it can also be applied to expressions.