What is the correct output of preprocessing the following 3 lines under the C99 rules?
#define y(x) x
#define x(a) y(a
x(1) x(2)))
BTW cpp under linux produces an error message, but I can’t see why the answer isn’t simply
1 2
Assuming cpp is correct and I’m wrong, I’d be very grateful for an explanation.
When a macro is found, the preprocessor gathers up the arguments to the macro and then scans each macro argument in isolation for other macros to expand within the argument BEFORE the first macro is expanded:
So in this specific example, it sees
x(1)and expands that, givingIt then identifies the macro call
y(1 x(2)), with the argument1 x(2)and prescans that for macros to expand. Within that it findsx(2)which expands toy(2and then triggers the error due to there not being a)for theymacro. Note at this point its still looking to expand the argument of the firstymacro, so its looking at it in isolation WITHOUT considering the rest of the input file, unlike the expansion that takes place for 6.10.3.4Now there’s some question as to whether this should actually be an error, or if the preprocessor should treat this
y(2sequence as not being a macro invocation at all, as there is no ‘)’. If it does the latter then it will expand that y call to1 y(2which will then be combined with the rest of the input ()) and ultimately expand to1 2