Sun Studio 12: C User's Guide

6.5.5 Using Strings


Note –

In ISO C, the examples below marked with a ? produce a warning about use of old features, when you use the -xtransition option. Only in the transition mode ( –Xt and -Xs) is the result the same as in previous versions of C.


In K&R C, the following code 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. ISO C recognized the importance of this feature, but could not condone operations on parts of tokens. In ISO C, all invocations of the above macro produce the string literal "a!". To achieve the old effect in ISO C, we make use of the # macro substitution operator and the concatenation of string literals.


#define str(a) #a "!"
str(x y)

The above code produces the two string literals "x y" and "!" which, after concatenation, produces the identical "x y!".

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 we know of is to change all uses of this macro to:


#define CNTL(ch) (037 & (ch))
CNTL(’L’)

This code is more readable and more useful, as it can also be applied to expressions.