Hi,
In ISO C/C++, a string constant prefixed by the letter `L' is a wide
string constant and is of type "array of wchar_t".
Consider the following C program fragment:
#include <stdio.h>
int main()
{
const wchar_t* my_string = L"a";
wprintf(my_string);
return 0;
}
This compiles fine (and even seems to work). Now, suppose that "a" is
defined through a macro (presumable in another source file):
#define STRING_LITERAL "a"
QUESTION: How does one define a wide-character string based on
`STRING_LITERAL'?
Naturally, this fails miserably:
const wchar_t* my_string = LSTRING_LITERAL;
because the compiler treats `LSTRING_LITERAL' as a single token.
This does not work either:
const wchar_t* my_string = L STRING_LITERAL;
because there may be no space between `L' and the string within double
quotes.
Any idea?
-- dave 7 4557
Dave Ohlsson wrote:
#define STRING_LITERAL "a"
QUESTION: How does one define a wide-character string based on `STRING_LITERAL'?
#define LPASTE(s) L##s
#define L(s) LPASTE(s)
wcout << L(STRING_LITERAL);
The two level macro is required so that STRING_LITERAL gets
substituted by "a" before you use the ## operator to glue
the L on the front of the token.
If you just did
#define L(s) L##s
L(STRING_LITERAL)
you'd get
LSTRING_LITERAL
as after substitution.
On Mon, 15 Nov 2004 12:11:20 -0800, Dave Ohlsson wrote: Hi,
In ISO C/C++, a string constant prefixed by the letter `L' is a wide string constant and is of type "array of wchar_t".
Consider the following C program fragment:
#include <stdio.h> int main() { const wchar_t* my_string = L"a"; wprintf(my_string); return 0; }
This compiles fine (and even seems to work). Now, suppose that "a" is defined through a macro (presumable in another source file):
#define STRING_LITERAL "a"
QUESTION: How does one define a wide-character string based on `STRING_LITERAL'?
Naturally, this fails miserably: const wchar_t* my_string = LSTRING_LITERAL; because the compiler treats `LSTRING_LITERAL' as a single token.
This does not work either: const wchar_t* my_string = L STRING_LITERAL; because there may be no space between `L' and the string within double quotes.
Any idea?
There may be a better way but the best I can come up with at the moment is
this:
const wchar_t *my_string = L""STRING_LITERAL
-- dave
Rob Gamble
On Mon, 15 Nov 2004 15:26:51 -0500, Ron Natalie wrote: Dave Ohlsson wrote:
#define STRING_LITERAL "a"
QUESTION: How does one define a wide-character string based on `STRING_LITERAL'? #define LPASTE(s) L##s #define L(s) LPASTE(s)
wcout << L(STRING_LITERAL);
Or for those of us who don't do c++ (not a criticism, I know this was
cross-posted to comp.lang.c++):
wprintf(L"%s\n", L(STRING_LITERAL));
(appropriate headers needed as well)
The two level macro is required so that STRING_LITERAL gets substituted by "a" before you use the ## operator to glue the L on the front of the token.
If you just did #define L(s) L##s L(STRING_LITERAL)
you'd get LSTRING_LITERAL as after substitution.
I like this approach as it can easily be extended to solve the
question naturally extending from this one which is how to perform the
similar task of using suffixes on macros expanding to numbers.
Rob Gamble
On Mon, 15 Nov 2004 12:11:20 -0800, Dave Ohlsson wrote: Hi,
In ISO C/C++, a string constant prefixed by the letter `L' is a wide string constant and is of type "array of wchar_t".
Consider the following C program fragment:
#include <stdio.h> int main() { const wchar_t* my_string = L"a"; wprintf(my_string); return 0; }
Now that your question has been answered, a couple of points:
1. int main (void) is preferred
2. you need to include <wchar.h> for wprintf
3. the wprintf statement is better written as:
wprintf(L"%ls\n", my_string); (or without the \n)
-- dave
Rob Gamble
Robert Gamble wrote: On Mon, 15 Nov 2004 12:11:20 -0800, Dave Ohlsson wrote:
Hi,
In ISO C/C++, a string constant prefixed by the letter `L' is a wide string constant and is of type "array of wchar_t".
Consider the following C program fragment:
#include <stdio.h> int main() { const wchar_t* my_string = L"a"; wprintf(my_string); return 0; }
Now that your question has been answered, a couple of points:
1. int main (void) is preferred
Not in C++ it isn't. Watch your cross-postings.
2. you need to include <wchar.h> for wprintf 3. the wprintf statement is better written as: wprintf(L"%ls\n", my_string); (or without the \n)
How is it better?
-- dave
Rob Gamble
V
On Mon, 15 Nov 2004 17:02:08 -0500, Victor Bazarov wrote: Robert Gamble wrote: On Mon, 15 Nov 2004 12:11:20 -0800, Dave Ohlsson wrote:
Hi,
In ISO C/C++, a string constant prefixed by the letter `L' is a wide string constant and is of type "array of wchar_t".
Consider the following C program fragment:
#include <stdio.h> int main() { const wchar_t* my_string = L"a"; wprintf(my_string); return 0; }
Now that your question has been answered, a couple of points:
1. int main (void) is preferred
Not in C++ it isn't. Watch your cross-postings.
In the sentence immediately preceding the code being discussed, the
author indicates the code is C, not C++ 2. you need to include <wchar.h> for wprintf 3. the wprintf statement is better written as: wprintf(L"%ls\n", my_string); (or without the \n)
How is it better?
Still talking C here (don't know about C++):
printf(string) is considered very bad practice in C since the string being
passed may not be a valid format string which the printf functions expect
as their first argument.
Additionally, and arguably more significant, is the fact that using the
single argument form with the printf functions presents a serious security
vulnerability resulting in potential buffer overflows if the appropriate
data finds it's way into the string being passed (such as %n).
V
Rob Gamble
Thanks to Ron and Robert for their insightful answers and comments!
-- dave This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christian Wittern |
last post by:
> Wide unicode is currently not supported on Windows. A number of
> internal APIs (in particular for the registry, and for the "mbcs"
> codec) assume that sizeof(Py_UNICODE) is 2. Contributions are...
|
by: Brian van den Broek |
last post by:
Hi all,
IDLE refuses to launch, and I believe it is because I attempted to
define a custom key-binding that it doesn't like.
I was recently defining a custom keybinding in IDLE 1.1 under...
|
by: Yaron Cohen |
last post by:
Hi,
I would like to ask for your help.
I am using IE5.5.
I have a wide page with horizontal scroll bar.
The problem is that I get only 1 page when printing it using
"file->print" or...
|
by: B McDonald |
last post by:
Hi. There is an inconsistency in the rendering of my side-by-side content
and nav columns across the browsers I've tested on. In particular, IE6
renders the margin between the two columns too...
|
by: Csaba Gabor |
last post by:
Hi, I have a wide table (horizontally overflows the page). With
a lot of wide entries, the narrow ones are getting squashed. How
can I set a minimum width in IE (since it doesn't like my...
|
by: Glenn Thimmes |
last post by:
I am needing to read and write application settings from within my ASP.NET
application. My web.config is not an option since I need to be able to write
settings as well. My database is not an...
|
by: Steven Bethard |
last post by:
> >>> aList =
> >>> it = iter(aList)
> >>> zip(it, it)
>
> That behavior is currently an accident.
>http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1121416
|
by: Seago |
last post by:
I'm not sure if I can explain my issue well enough, but here goes:
I want to define an .XSD such that it allows for an ( A | B )*
configuration where A and B are different elements. I would like...
|
by: jeff_j_dunlap |
last post by:
Hello,
Whenever I need class wide access to an object, I declare it
dynamically:
class myClass
{
...
myObject* obj; // declared dynamically
...
|
by: Louis |
last post by:
Hello,
My website is 910 pixels wide. The content area is 552 pixels wide. If
I place a picture in the content area that is wider than 552 pixels my
site becomes wider than 910 pixels. I do not...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |