Preprocessor macro to quote its argument

Frank P. Bresz fpb at ittc.wec.com
Sun Aug 19 06:35:34 AEST 1990


In article <1112 at mti.mti.com> adrian at mti.mti.com (Adrian McCarthy) writes:

>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.

	Funny I have been using the following debug.h for years without
much problem.  I use the argument a inside quotes and it expands exactly as
I want it to.   You have to be carefull about side effects because it
evaluates twice.  Once for the printing and once to get the result this
allows 
	a = dbgdec(x);

	to work correctly.  I use the dbgmsg[n] to print out informational
messages to stderr.  I name everything dbg so I can quickly nuke them when
I am done debugging.

	BTW This is on SunOS I have also done it on an INTEL 310 system
running some XENIXy kind of unix.


debug.h 

#define dbgflt(a) (fprintf(stderr,"a = %f\n",a),fflush(stderr),(a))
#define dbghex(a) (fprintf(stderr,"a = %08lx\n",a),fflush(stderr),(a))
#define dbghexs(a) (fprintf(stderr,"a = %04x\n",a),fflush(stderr),(a))
#define dbghexc(a) (fprintf(stderr,"a = %02x\n",(unsigned char) a),fflush(stderr),(a))
#define dbgdec(a) (fprintf(stderr,"a = %12ld\n",a),fflush(stderr),(a))
#define dbgdecs(a) (fprintf(stderr,"a = %6d\n",a),fflush(stderr),(a))
#define dbgdecc(a) (fprintf(stderr,"a = %3d\n",(unsigned char) a),fflush(stderr),(a))
#define dbgmsg(a) (fprintf(stderr,"%s\n",a),fflush(stderr),(a))
#define dbgmsgn(a) (fprintf(stderr,"%s",a),fflush(stderr),(a))
#define dbgsev(a) (fprintf(stderr,"a = '%s'\n",a),fflush(stderr),(a))
#define dbgfile	(fprintf(stderr,"%s:%d",__FILE__,__LINE__),fflush(stderr))

	It allows me to put the following in my C code and I will get
results below:

...

int foo;
char *str;

dbgdec(foo);
....
dbghex(str);
dbgsev(str);

....

Execution is 

...
foo = 15
...
str = 0x00049812
str = 'This is a test'
--
+--------------------+
|fbresz at ittc.wec.com |  My opinions are my own, I'm not paid
|uunet!ittc!fbresz   |  enough to make an official statement  
|(412)733-6749       |  +-----------------------------------+
|Fax: (412)733-6444  |  |      THIS SPACE FOR SALE!!!       |
+--------------------+  +-----------------------------------+



More information about the Comp.lang.c mailing list