Macro string argumentation for a wide string literal in a C / C ++ preprocessor

Preprocessor

C has a stringification function . This is a function that allows you to create a (narrow) string literal from a macro parameter. It can be used as follows:

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

Usage example:

PRINTF_SIZEOF(int);

... can 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?> )
+4
source share
1 answer

To get a string literal from a macro argument, you need to combine the string with concatenation .

WPRINTF_SIZEOF can be defined as:

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

() , :

#define WSTR(x) L ## #x
#define WPRINTF_SIZEOF(x) wprintf(L"sizeof(%s) == %d", WSTR(x), sizeof(x))
+4

Source: https://habr.com/ru/post/1657907/