0

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
user1850250
  • 23
  • 1
  • 3
  • See http://stackoverflow.com/questions/6671698/adding-quotes-to-argument-in-c-preprocessor, it's a similar question to yours. – Jacob Pollack Aug 13 '13 at 21:17
  • Why do you think you need to use a pre-standard (pre-C89) preprocessor? – Jonathan Leffler Aug 13 '13 at 21:19
  • I am using gfortran which uses the traditional preprocessor. I know I could first process my files using gcc and then compile these with gfortran, but I am exploring whether it is possible to avoid this – user1850250 Aug 13 '13 at 21:22
  • Fair enough — if that's what `gfortran` does, there's not much you can do about it. Question for you: is what you're doing related to included files (`#include` directives) as discussed in [SO 6671698](http://stackoverflow.com/q/6671698), mentioned in another comment? Or is it unrelated to that? What is the ultimate goal to be achieved? There might be another way to handle it — or there might not. (Are we dealing with an [XY Problem](http://mywiki.wooledge.org/XyProblem), in fact?) – Jonathan Leffler Aug 14 '13 at 00:36

1 Answers1

1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278