12

C preprocessor has a feature called stringification. It's a feature that allows to create a (narrow) string literal from a macro parameter. It can be used like this:

#define PRINTF_SIZEOF(x) printf("sizeof(%s) == %d", #x, sizeof(x))
/*                                  stringification ^^          */

Usage example:

PRINTF_SIZEOF(int);

...might print:

sizeof(int) == 4

How to create a wide string literal from a macro parameter? In other words, how can I implement WPRINTF_SIZEOF?

#define WPRINTF_SIZEOF(x) wprintf( <what to put here?> )
cubuspl42
  • 7,833
  • 4
  • 41
  • 65
  • 2
    Self-answering is fine, but this has been answered/discussed many times before on SO. See one here: http://stackoverflow.com/questions/216875/what-are-the-applications-of-the-preprocessor-operator-and-gotchas-to-conside – P.P Oct 16 '16 at 13:00
  • 1
    I actually can't find a dupe. – Lightness Races in Orbit Oct 16 '16 at 13:10
  • @P.P. I can't see your point. If you state that provided question is a duplicate, I strongly disagree. Otherwise, please provide a link to a duplicate. – cubuspl42 Oct 16 '16 at 13:18
  • Your question isn't really about wide strings but about how to put two tokens together. If you search [token concatenation](http://stackoverflow.com/search?q=token+concatenation) and [token pasting](http://stackoverflow.com/search?q=token+pasting) there are 529 and 714 hits. I am certainly not interested in finding a dupe that's agreeable to you. But the "point" is there's nothing new/interesting that requires a new post (it's just another one among the sea of dupes on SO). – P.P Oct 16 '16 at 13:49
  • I didn't create this question to gather unicorn points, but because I could't find the answer to my question on stackoverflow. The only thing I cared about was to wide-stringify a macro param in the project I'm working on (my environment lacks narrow-char printf). I *didn't* know that the answer is to use token concatenation. You are more experienced; you did know. But the argument like "Your question X doesn't make sense; all you needed to do is to search for Y, where Y is answer to your question" is ridiculous. – cubuspl42 Oct 16 '16 at 14:17

1 Answers1

9

In order to produce a wide string literal from a macro argument, you need to combine stringification with concatenation.

WPRINTF_SIZEOF can be defined as:

#define WPRINTF_SIZEOF(x) wprintf(L"sizeof(%s) == %d", L ## #x, sizeof(x))
/*                                         concatenation ^^ ^^ stringification */

In order to (arguably) increase readability, you can extract this trick into a helper macro:

#define WSTR(x) L ## #x
#define WPRINTF_SIZEOF(x) wprintf(L"sizeof(%s) == %d", WSTR(x), sizeof(x))
cubuspl42
  • 7,833
  • 4
  • 41
  • 65
  • btw you probably want a `\n` there! – Lightness Races in Orbit Oct 16 '16 at 13:11
  • 2
    In both C and C++ `#define WSTR(x) L ## #x` is wrong because it relies on unspecified behavior: C11 (J.1 Unspecified behavior): _The order in which # and ## operations are evaluated during macro substitution_, C++, N4713 (19.3.2 The # operator): _The order of evaluation of # and ## operators is unspecified_. One possible correct implementation is: `#define CAT_(x,y) x ## y #define CAT(x,y) CAT_(x,y) #define STR_(x) #x #define STR(x) STR_(x) #define WSTR(x) CAT(L,STR(x))`. – pmor Apr 21 '21 at 13:13