defining wide-character strings with macros

D

Dave Ohlsson

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
 
R

Ron Natalie

Dave said:
#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.
 
R

Robert Gamble

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

Rob Gamble
 
R

Robert Gamble

#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
 
V

Victor Bazarov

Robert said:
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?
Rob Gamble

V
 
R

Robert Gamble

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)

Rob Gamble
 
R

Robert Gamble

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++
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).

Rob Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top