[net.lang.c] Hygiene of #define DIR "/usr/lib/news

ado@elsie.UUCP (Arthur David Olson) (02/12/85)

The 2.11 vnews source posted lately had a thought-provoking way of doing
string catenation.  It had defines such as

	#define DIR "/usr/lib/news

and used these defines in lines such as

	system(DIR/batch");

which, after being run through the 4.1bsd C preprocessor, yields

	system("/usr/lib/news/batch");

I'm wondering what folks think of the hygiene and portability of this
technique for accomplishing string catenation.  Let me know.
--
	UUCP: ..decvax!seismo!elsie!ado    ARPA: elsie!ado@seismo.ARPA
	DEC, VAX and Elsie are Digital Equipment and Borden trademarks

jans@mako.UUCP (Jan Steinman) (02/15/85)

In article <5053@elsie.UUCP> ado@elsie.UUCP (Arthur David Olson) writes:
>... a thought-provoking way of doing string catenation...
>
>	#define DIR "/usr/lib/news
>
>and used these defines in lines such as
>
>	system(DIR/batch");
>
Most pre-processors work with whitespace delimited tokens, so the above will
not always work.  However, tokens are not allowed to contain embedded
comments, so this is a more portable way to do the trick:

	system(DIR/**//batch");

I use this technique regularly to produce unique labels for assembly macro
expansion:

	#define MY_MACRO(garbage_in)		\
		.globl my_routine/**/garbage_in	\
	my_routine/**/garbage_in:		\
		...(assembly code)...		\
	/* end define MY_MACRO */
-- 
:::::: Jan Steinman		Box 1000, MS 61-161	(w)503/685-2843 ::::::
:::::: tektronix!tekecs!jans	Wilsonville, OR 97070	(h)503/657-7703 ::::::

henry@utzoo.UUCP (Henry Spencer) (02/22/85)

> ...  However, tokens are not allowed to contain embedded
> comments, so this is a more portable way to do the trick:
> 
> 	system(DIR/**//batch");

This isn't any more portable.  *Any* technique that relies on unclosed
strings depends on the exact implementation of the preprocessor.  And
anything that depends on /**/ disappearing from the middle of (what is
intended to become) a single token depends specifically on the Reiser
preprocessor, found in the Bell compilers and nowhere else.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry