0

I have a define that looks like this:

#define HELLO "hello"

I wanted to make "hello" a wide string without having to type it again, so I tried with this macro and hoped for the best:

#define WIDEN(x) L ## x
#define WIDE_HELLO WIDEN(HELLO)

However, this expands to LHELLO.

I found other examples of this on the Internet, and they use an intermediate macro and it works:

#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define WIDE_HELLO WIDEN(HELLO)

What does the intermediate macro do that solves the problem?

RedRidingHood
  • 568
  • 3
  • 16
zneak
  • 134,922
  • 42
  • 253
  • 328
  • The keywords you're probably looking for are "macro argument prescan". [Here's a pretty good explanation of how it works, from the GCC docs.](https://gcc.gnu.org/onlinedocs/cpp/Argument-Prescan.html) – Ilmari Karonen Aug 19 '16 at 19:11
  • 1
    During expansion of a function-like macro `M`, the arguments are first expanded completely, **except** where they are operands of the `#` or `##` operator in the replacement text of `M`. Afterward, `M`'s replacement text is rescanned for additional macros to expand. In the direct token pasting case, the `##` operator suppresses the expansion of the argument. In the indirect case, the argument is fully expanded, and the replacement text contains another macro invocation with the expanded text as its argument. That gets expanded to what you want on rescan. – John Bollinger Aug 19 '16 at 19:13

0 Answers0