Preprocessor Usage Question

Jonathan I. Kamens jik at athena.mit.edu
Fri Apr 26 09:43:40 AEST 1991


  Many pre-ANSI-C preprocessors will let you use /**/ to do concatenation as
Gary Weimer has described.  Some ANSI-C preprocessors will let you do it too,
but it's the WRONG way to do it with an ANSI-C preprocessor; under ANSI C, you
should use the ## macro operator.

  I took the following source file:

	#define R1(x) a_/**/x
	#define R2(x) a_##x
	#define R3 a_

	R1(func1)(args);
	R2(func1)(args);
	R3/**/func1(args);
	R3##func1(args);

And fed it into 4.3BSD's preprocessor, and got out this (blank lines and #
line number directives deleted):

	 a_func1(args);
	 a_##func1(args);
	a_func1(args);
	a_##func1(args);
	pit-manager% cc -E -Hnocpp test.c

So, under 4.3BSD, the /**/ trick works, and ANSI C's ## doesn't.  Then, I took
the same file and fed it into High C's internal ANSI-C preprocessor, which
resulted in:

	a_ func1 (args);
	a_func1  (args);
	a_    func1(args);
	a_##func1(args);

Note first of all that the /**/ DIDN'T work, because extra blank space was
left where the /**/ appeared.  This is perfectly permissible according to the
ANSI C specification.  The ## operator worked, but only when it was used
inside a macro, since that's when it's supposed to work.

  I guess the summary of all this is that something like this

	#ifdef __STDC__
	#define RADIO(x) a_##x
	#else
	#define RADIO(x) a_/**/x
	#endif

is the best you can do.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.questions mailing list