开发者

How to concatenate two wide strings using a macro in C?

I want to concatenate two wide strings using a macro, so I define some macros:

#define VERSION_MAJOR 1
#define VERSION_MINOR 1
#define VERSION_BUILD 0
#d开发者_高级运维efine VERSION_REVISION 0


#define _STR(s) #s
#define STR(s) _STR(s)

#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)


//http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
#define PRODUCT_ELASTOS_VERSION STR(VERSION_MAJOR) "." \
                                STR(VERSION_MINOR) "." \
                                STR(VERSION_BUILD) "." \
                                STR(VERSION_REVISION)

now I want to define a new macro PRODUCT_ELASTOS_VERSION_W using macro PRODUCT_ELASTOS_VERSION, it's value should be L"1.1.0.0". so how can I define this macro? TOWSTRING(PRODUCT_ELASTOS_VERSION) is wrong answer.

And if I want to concatenating string, how should I write? L"v" TOWSTRING(PRODUCT_ELASTOS_VERSION) cann't get wide string L"v1.1.0.0".


First, PRODUCT_ELASTOS_VERSION does not expand to "1.1.0.0", it expands to

"1" "." "1" "." "0" "." "0"

Keeping the same structure you can define another identifier that expands to

L"1" L"." L"1" L"." L"0" L"." L"0"

with

#define _LSTR(s) L ## #s
#define LSTR(s) _LSTR(s)

#define ANOTHER_IDENTIFIER LSTR(VERSION_MAJOR) L"." \
                           LSTR(VERSION_MINOR) L"." \
                           LSTR(VERSION_BUILD) L"." \
                           LSTR(VERSION_REVISION)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜