[comp.std.c] macro expansion

bright@Data-IO.COM (Walter Bright) (02/07/89)

I'm having a bit of trouble understanding macro expansion under ANSI C.
Given the source:

	#define A A) + A
	#define str(s) #s
	#define xstr(s) str(s)

Does xstr(A) expand to:
1)	"A" + A)
or:
2)	"A) + A"

If you say 1), then this is very hard to implement due to the rule that
however macros nest, circular macro references won't be expanded. The reason
is that 1) comes about because:
	xstr(A)
	str(A) + A)
Note at this stage that the expansion for A occurs both as a parameter and
as a 'tail' occuring afterwards. Very clumsy and confusing.

BTW, things would be much simpler if ANSI stated that the arguments to
a macro are not macro expanded until *AFTER* the macro they are an
argument to is expanded. I.e.
	#define A 1
	#define str(s) #s
	#define xstr(s) str(s)

xstr(A) expands to:
	"1"
instead of the more reasonable:
	"A"