سؤال

If I am using standard preprocessing then I may perform an indirect quote by:

#define foo bar
#define quoteme_(x) #x
#define quoteme(x) quoteme_(x)

and then just use quoteme(foo) to obtain "bar"

I want to do this, but using the pre-processor in traditional mode. I have attempted to just replace #x with 'x' but quoteme(foo) just returns 'foo'.

Any help is much appreciated.

هل كانت مفيدة؟

المحلول

Using the cpp that comes with GCC (4.8.1 tested), and the code (example.c):

#define foo bar
#define quoteme_(x) "x"
#define quoteme(x) quoteme_(x)

quoteme(foo)

The relevant part of the output from cpp -traditional example.c is:

"foo"

(and you can use single quotes in the replacement for quoteme_(x) similarly to obtain 'foo'). This is what you observed in the question.

AFAIK, there isn't a way to get 'bar' or "bar" out of the traditional preprocessor system. The pre-standard (traditional) systems were not standardized, and there were details where different systems behaved differently. However, macro arguments were expanded after replacement, rather than before as in C89 and later, which is what leads to the result you're seeing.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top