Connecting Tech Pros Worldwide Forums | Help | Site Map

defining wide-character strings with macros

Dave Ohlsson
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Ron Natalie
Guest
 
Posts: n/a
#2: Jul 22 '05

re: defining wide-character strings with macros


Dave Ohlsson wrote:[color=blue]
>
>
> #define STRING_LITERAL "a"
>
> QUESTION: How does one define a wide-character string based on
> `STRING_LITERAL'?[/color]

#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.
Robert Gamble
Guest
 
Posts: n/a
#3: Jul 22 '05

re: defining wide-character strings with macros


On Mon, 15 Nov 2004 12:11:20 -0800, Dave Ohlsson wrote:
[color=blue]
> 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?[/color]

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
[color=blue]
> -- dave[/color]

Rob Gamble

Robert Gamble
Guest
 
Posts: n/a
#4: Jul 22 '05

re: defining wide-character strings with macros


On Mon, 15 Nov 2004 15:26:51 -0500, Ron Natalie wrote:
[color=blue]
> Dave Ohlsson wrote:[color=green]
>>
>>
>> #define STRING_LITERAL "a"
>>
>> QUESTION: How does one define a wide-character string based on
>> `STRING_LITERAL'?[/color]
>
> #define LPASTE(s) L##s
> #define L(s) LPASTE(s)
>
> wcout << L(STRING_LITERAL);[/color]

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)
[color=blue]
> 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.[/color]

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
Robert Gamble
Guest
 
Posts: n/a
#5: Jul 22 '05

re: defining wide-character strings with macros


On Mon, 15 Nov 2004 12:11:20 -0800, Dave Ohlsson wrote:
[color=blue]
> 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;
> }[/color]

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)
[color=blue]
> -- dave[/color]

Rob Gamble
Victor Bazarov
Guest
 
Posts: n/a
#6: Jul 22 '05

re: defining wide-character strings with macros


Robert Gamble wrote:[color=blue]
> On Mon, 15 Nov 2004 12:11:20 -0800, Dave Ohlsson wrote:
>
>[color=green]
>>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;
>> }[/color]
>
>
> Now that your question has been answered, a couple of points:
>
> 1. int main (void) is preferred[/color]

Not in C++ it isn't. Watch your cross-postings.
[color=blue]
> 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)[/color]

How is it better?
[color=blue]
>
>[color=green]
>>-- dave[/color]
>
>
> Rob Gamble[/color]

V
Robert Gamble
Guest
 
Posts: n/a
#7: Jul 22 '05

re: defining wide-character strings with macros


On Mon, 15 Nov 2004 17:02:08 -0500, Victor Bazarov wrote:
[color=blue]
> Robert Gamble wrote:[color=green]
>> On Mon, 15 Nov 2004 12:11:20 -0800, Dave Ohlsson wrote:
>>
>>[color=darkred]
>>>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;
>>> }[/color]
>>
>>
>> Now that your question has been answered, a couple of points:
>>
>> 1. int main (void) is preferred[/color]
>
> Not in C++ it isn't. Watch your cross-postings.[/color]

In the sentence immediately preceding the code being discussed, the
author indicates the code is C, not C++
[color=blue][color=green]
>> 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)[/color]
>
> How is it better?[/color]

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).
[color=blue]
> V[/color]

Rob Gamble
Dave Ohlsson
Guest
 
Posts: n/a
#8: Jul 22 '05

re: defining wide-character strings with macros


Thanks to Ron and Robert for their insightful answers and comments!

-- dave
Closed Thread