DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Complying with standard C

Stringizing


NOTE: In ANSI C, the examples below marked with a ++ produce a warning about use of old features. Only in the transition mode (-Xt) will the result be the same as in previous versions of C.

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.
Next topic: Token pasting
Previous topic: Macro replacement

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005