defining wide-character strings with macros

Discussion in 'C++' started by Dave Ohlsson, Nov 15, 2004.

  1. Dave Ohlsson

    Dave Ohlsson Guest

    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
    Dave Ohlsson, Nov 15, 2004
    #1
    1. Advertising

  2. Dave Ohlsson

    Ron Natalie Guest

    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.
    Ron Natalie, Nov 15, 2004
    #2
    1. Advertising

  3. 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
    Robert Gamble, Nov 15, 2004
    #3
  4. 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
    Robert Gamble, Nov 15, 2004
    #4
  5. 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
    Victor Bazarov, Nov 15, 2004
    #5
  6. 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, Nov 15, 2004
    #6
  7. 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
    Robert Gamble, Nov 16, 2004
    #7
  8. Dave Ohlsson

    Dave Ohlsson Guest

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

    -- dave
    Dave Ohlsson, Nov 16, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web Developer

    char 8bit wide or 7bit wide in c++?

    Web Developer, Jul 31, 2003, in forum: C++
    Replies:
    2
    Views:
    571
    John Harrison
    Jul 31, 2003
  2. Dave Ohlsson

    defining wide-character strings with macros

    Dave Ohlsson, Nov 15, 2004, in forum: C Programming
    Replies:
    7
    Views:
    355
    Dave Ohlsson
    Nov 16, 2004
  3. George2
    Replies:
    2
    Views:
    364
    James Kanze
    Jan 25, 2008
  4. Disc Magnet
    Replies:
    2
    Views:
    695
    Jukka K. Korpela
    May 15, 2010
  5. Disc Magnet
    Replies:
    2
    Views:
    778
    Neredbojias
    May 14, 2010
Loading...

Share This Page