Preprocessor macro to quote its argument

rankin at eql.caltech.edu rankin at eql.caltech.edu
Mon Aug 20 14:48:02 AEST 1990


[ He wants to turn a macro argument into a quoted string, which is done
significantly differently in ANSI C than in 'traditional' C. ]

In article <1114 at mti.mti.com>, adrian at mti.mti.com (Adrian McCarthy) writes:
>[text deleted]
> For ANSI preprocessors, use:
> 	#define Q1(x)	#x
> For pre-ANSI preprocessors, the original solution *may* work.  Reportedly,
> 	#define Q1(x)	" x "
> may even work with many pre-ANSI preprocessors, though not VAX C.

     The second method works fine in VAX C (which is not [yet?] ANSI
compliant).  The preprocessor is just an internal phase of the integrated
compiler, but that doesn't make any difference in a case like this.

$  type t.c   			!t.c is a trivial test file
#define Q1(x) " x "
char *string = Q1(xyzzy);
$  cc/preprocess_only t		!read t.c, write t.i
$  type t.i			!display the result
# 1 "USER:[RANKIN]T.C;1"

char *string = " xyzzy ";

     Older versions of the compiler didn't support an explicit preprocess-
only option, but they still did the macro substitution this way.

		Pat Rankin, rankin at eql.caltech.edu



More information about the Comp.lang.c mailing list