How to convert concatenated strings to wide-char with the C preprocessor?
2012-05-25 13:12
459 查看
If you want to convert a character string literal to a wide character string literal, you properly define a macro as _L here:
[align=left] [/align]
[align=left]#define _L(x) __L(x)[/align]
[align=left]#define __L(x) L ## x[/align]
[align=left]#define STR "I am handsome"[/align]
[align=left] [/align]
[align=left]wchar_t str1[] = _L("I am handsome" );[/align]
[align=left]wchar_t str2[] = _L(STR);[/align]
It works fine at most all compiler currently. However, when use it to convert the concatenated string like
[align=left]#define STR3 "I am handsome\n" \[/align]
[align=left] "I love u\n"[/align]
[align=left] [/align]
[align=left]wchar_t str3[] = _L(STR3);//error C2308: concatenating mismatched strings[/align]
you will get a compiler error in MSVC.
This is because it's the new feature in C99 standards. The MSVC don't support it for a long time.
If uses the GCC, the second code piece get compiled without error.
Look how C99 standard said.
[align=left]6.4.5 String literals[/align]
[align=left]In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any f the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal; otherwise, it is treated as a character string literal.[/align]
[align=left] [/align]
[align=left]#define _L(x) __L(x)[/align]
[align=left]#define __L(x) L ## x[/align]
[align=left]#define STR "I am handsome"[/align]
[align=left] [/align]
[align=left]wchar_t str1[] = _L("I am handsome" );[/align]
[align=left]wchar_t str2[] = _L(STR);[/align]
It works fine at most all compiler currently. However, when use it to convert the concatenated string like
[align=left]#define STR3 "I am handsome\n" \[/align]
[align=left] "I love u\n"[/align]
[align=left] [/align]
[align=left]wchar_t str3[] = _L(STR3);//error C2308: concatenating mismatched strings[/align]
you will get a compiler error in MSVC.
This is because it's the new feature in C99 standards. The MSVC don't support it for a long time.
If uses the GCC, the second code piece get compiled without error.
Look how C99 standard said.
[align=left]6.4.5 String literals[/align]
[align=left]In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any f the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal; otherwise, it is treated as a character string literal.[/align]
相关文章推荐
- How to Render the Hyperlink with Braces
- How to align content of a div to the bottom with css?
- How To Goods Movements with BAPI ==>> Calling the BAPI several times within one roll area
- How to work with the snap environment
- How To Use the PivotTable Office Web Component with VB (转载自微软)
- How to sync a fork repo to keep it up-to-date with the upstream repo on Windows?
- How to get the length of array of strings in C? - Yahoo! Answers
- [MSDN]How to use the computer keyboard with Windows Phone Emulator
- How to realize the UIView convert to UIImage?
- How to Train a ChatBot with the TensorFlow and Google Cloud ML
- How To Use the Spreadsheet Web Component with Visual Basic
- How to achieve low latency with the 10Gbps Ethernet
- How to use the cut command with examples
- How to craw the Info of BiliBIli with python
- How to change the text with back button?
- How to add subview to a webview so that the subview would scroll along with webview?
- urllib2 - The Missing Manual HOWTO Fetch Internet Resources with Python
- How to execute the command with root jurisdiction in pyCharm
- How to replace the boot screen with one of your own
- How to Share a Theme With All Users in a Windows 7 Computer?