Preprocessor Trickery

jwp at sdchema.UUCP jwp at sdchema.UUCP
Fri Jul 13 12:55:49 AEST 1984


...!elsie!ado:

	>Try typing this to see what happens:
	>	cc -E -
	>	#define Ident(xxx) xxx
	>	#define concat(a,b) Ident(a)b
	>	__LINE__
	>	concat(__LINE__,__LINE__)

"concat(__LINE__,__LINE__)" gives "__LINE____LINE__"
"concat(__LINE__, __LINE__)" gives, e.g.,  "3 3"

For that matter:	"concat(this,that)" results in "thisthat"
			"concat(this, that)" results in "this that"

Defining "concat(a, b)" instead of "concat(a,b)" doesn't, however, seem to
make a difference.  It *is* interesting behavior.  And also reinforces my
general feelings that:  (a) relying on the preprocessor to do too much for you
is foolish, and (b) being very clever is a really fun way to screw up those
that follow you (or yourself when you have to go back to the miserable thing
two years later and have forgotten how terribly clever you were), especially
when you follow the customary C commenting conventions.

				John Pierce, Chemistry, UC San Diego
				sdcsvax!sdchema!jwp



More information about the Comp.lang.c mailing list