Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > defining wide-character strings with macros

Reply
Thread Tools

defining wide-character strings with macros

 
 
Dave Ohlsson
Guest
Posts: n/a
 
      11-15-2004
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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      11-15-2004
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.
 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      11-15-2004
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

 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      11-15-2004
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-15-2004
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
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      11-15-2004
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
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      11-16-2004
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
 
Reply With Quote
 
Dave Ohlsson
Guest
Posts: n/a
 
      11-16-2004
Thanks to Ron and Robert for their insightful answers and comments!

-- dave
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ruby.h defining macros John Ky Ruby 2 02-12-2009 05:13 AM
defining wide-character strings with macros Dave Ohlsson C Programming 7 11-16-2004 07:17 AM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
Explanation of macros; Haskell macros mike420@ziplip.com Python 80 11-07-2003 02:22 AM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 1 10-07-2003 04:07 PM



Advertisments