Preprocessor macro to quote its argument

Adrian McCarthy adrian at mti.mti.com
Sat Aug 18 12:20:58 AEST 1990


Ever needed a preprocessor macro that could quote its argument?  I did for
my own version of assert().  The obvious first try is:

	#define Q1(x)	" x "

but the argument isn't expanded because it's in quotes.  Solution follows...

	#define Q	"
	#define Q1(x)	Q x "

It introduces a few spaces, but fortunately that doesn't conflict with my
needs.

Note that:

	#define Q1(x)	Q x Q

does *not* work.  The second Q won't be translated.

I think my solution is portable.  It seems to comply with the ANSI
preprocessing notes in K&R/2.  In addition, it works on the non-ANSI
Sun and VAX/VMS compilers.

In case the motivation isn't clear, I wanted something along the lines of:

	ASSERT((x > 0))

to produce:

	assert((x > 0), " (x > 0) ")

so that the assert() function could print the assertion if the condition
fails.

Aid.  (adrian at gonzo.mti.com)



More information about the Comp.lang.c mailing list