Oracle® Solaris Studio 12.4: C User's Guide

Exit Print View

Updated: March 2015
 
 

6.4.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, use the # macro substitution operator and the concatenation of string literals.

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

This code produces the two string literals "x y" and "!" which, after concatenation, produce 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 example:

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

This example produces the following result, which evaluates to the ASCII control-L character.

(037 & ’L’)

The best solution is to change all uses of this macro as follows:

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

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