Connecting Tech Pros Worldwide Help | Site Map

defining wide-character strings with macros

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 09:54 PM
Dave Ohlsson
Guest
 
Posts: n/a
Default defining wide-character strings with macros

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

  #2  
Old July 22nd, 2005, 09:54 PM
Ron Natalie
Guest
 
Posts: n/a
Default 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.
  #3  
Old July 22nd, 2005, 09:54 PM
Robert Gamble
Guest
 
Posts: n/a
Default 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

  #4  
Old July 22nd, 2005, 09:55 PM
Robert Gamble
Guest
 
Posts: n/a
Default 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
  #5  
Old July 22nd, 2005, 09:55 PM
Robert Gamble
Guest
 
Posts: n/a
Default 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
  #6  
Old July 22nd, 2005, 09:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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
  #7  
Old July 22nd, 2005, 09:55 PM
Robert Gamble
Guest
 
Posts: n/a
Default 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
  #8  
Old July 22nd, 2005, 09:55 PM
Dave Ohlsson
Guest
 
Posts: n/a
Default Re: defining wide-character strings with macros

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

-- dave
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.