Preprocessor macro to quote its argument

Stephen Clamage steve at taumet.com
Wed Aug 22 01:03:38 AEST 1990


schaefer at ogicse.ogi.edu (Barton E. Schaefer) writes:

>Now wait a minute here -- is it really true that the "right hand side"
>of a #define constant must be a valid token sequence?  I was under the
>impression that only the result after expansion had to be a valid sequence.

The #define must consist of valid *pre-processor* tokens, which are not
quite identical to C language tokens.  In particular, partial string
constants and partial character constants are not allowed.  That is,
your examples such as of
	#define w	"
	#define x	abc"
	#define y	'
	#define z	d'
are all illegal in ANSI C.  (Preprocessor tokens include fragments of
file names in unexpanded #include directives, and fragments of
numbers in macro definitions.)

The "stringizing" and "token concatenation" preprocessor operators are
sufficient to do what the nonportable earlier constructs did, except
for building a character constant.  There are now no legal tricks in an
ANSI-compliant program which allow you to build a character constant
from parts.  You could write to the ANSI committee and suggest that the
next C standard (1995?) include "character-constant pasting" :-)
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list