How do I concatenate wide string literals with PRId32, PRIu64, etc.?

c, macros, string, unicode, visual-c++

Solution

The problem is twofold. First, as noted in rici's answer, C99 and C++11 both added support for concatenating narrow string literals and wide string literals together, so you should not need to widen the narrow literal by prepending the L. Visual C++ does not yet support this feature for either C or C++.

Because the compiler does not yet support this feature, we should in the libraries make it possible for you to explicitly widen these string literals using a technique like the one in your answer. Unfortunately, we've defined these macros in a way such that they may expand to multiple string literals. E.g., `PRId32` expands to `"l" "d"`.

This is valid, but it does prevent you from widening, because there is no way to prepend an `L` to the second string literal (to make `"d"` into `L"d"`). I'm afraid I don't see a way to make this work without (re)defining the macros yourself.

I've opened a bug internally so that if the compiler does not add support for concatenation of mixed-width literals during preprocessing in the next release, we can revisit these definitions to possibly make it possible to widen them explicitly.

Problem

Suppose I need to print a formatted string with an `int32_t` using the printf format specifier from `<inttypes.h>`. ``` int32_t i = 0; printf("%" PRId32 "\n", i); ``` If I try to do the same with wide characters in Visual C++ 2013, it fails: ``` #define W_(val) L ## val #define W(val) W_(val) wprintf(L"%" W(PRId32) L"\n", i); ``` error C2308: concatenating mismatched strings How do I concatenate wide string literals with format conversion specifier macros?

Original source