locale and wchar_t character constant

Discussion in 'C Programming' started by Owner, Feb 10, 2011.

  1. Owner

    Owner Guest

    what's wrong with this code?

    #include <locale.h>
    #include <stdio.h>
    #include <wchar.h>

    main(){
    setlocale(LC_CTYPE, "");
    wchar_t a = L'ÇÑ';
    putwchar(a);

    }

    result

    test.c:7: warning: multi-character character constant
     
    Owner, Feb 10, 2011
    #1
    1. Advertising

  2. Owner <> writes:

    > what's wrong with this code?
    >
    > #include <locale.h>
    > #include <stdio.h>
    > #include <wchar.h>
    >
    > main(){

    int main(void)

    > setlocale(LC_CTYPE, "");
    > wchar_t a = L'한';
    > putwchar(a);
    >
    > }
    >
    > result
    >
    > test.c:7: warning: multi-character character constant


    It works for me (as I'd expect). I image that the particular multi-byte
    sequence you have is not being recognised by the compiler.

    --
    Ben.
     
    Ben Bacarisse, Feb 10, 2011
    #2
    1. Advertising

  3. Owner

    Owner Guest

    On Thu, 10 Feb 2011 13:01:02 +0000, Ben Bacarisse wrote:

    > Owner <> writes:
    >
    >> what's wrong with this code?
    >>
    >> #include <locale.h>
    >> #include <stdio.h>
    >> #include <wchar.h>
    >>
    >> main(){

    > int main(void)
    >
    >> setlocale(LC_CTYPE, "");
    >> wchar_t a = L'ÇÑ';
    >> putwchar(a);
    >>
    >> }
    >>
    >> result
    >>
    >> test.c:7: warning: multi-character character constant

    >
    > It works for me (as I'd expect). I image that the particular multi-byte
    > sequence you have is not being recognised by the compiler.


    So you mean I should switch my tcc compiler to better compiler?
     
    Owner, Feb 10, 2011
    #3
  4. Owner <> writes:
    > what's wrong with this code?
    >
    > #include <locale.h>
    > #include <stdio.h>
    > #include <wchar.h>
    >
    > main(){


    Should be "int main(void) {", but that's not the problem.

    > setlocale(LC_CTYPE, "");
    > wchar_t a = L'한';
    > putwchar(a);
    >
    > }
    >
    > result
    >
    > test.c:7: warning: multi-character character constant


    It's hard to tell. Your article appears to have an euc-kr encoding
    (judging by the "Content-Type:" header on your article), but
    that could have been introduced by some news software somewhere.
    Is your original source file encoded in euc-kr?

    Probably your compiler doesn't recognize the character encoding you
    used in your source file, and is interpreting the bytes between the
    single quotes as two 8-bit characters, producing a multi-character
    character constant.

    Note that a call to setlocale() (which occurs at run time) cannot
    affect the compiler's interpretation of source characters.

    See what your compiler's documentation says about source character
    sets. You might hae better luck using UTF-8.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 10, 2011
    #4
  5. Owner <> writes:

    > On Thu, 10 Feb 2011 13:01:02 +0000, Ben Bacarisse wrote:
    >> Owner <> writes:

    <snip>
    >>> #include <locale.h>
    >>> #include <stdio.h>
    >>> #include <wchar.h>
    >>>
    >>> main(){

    >> int main(void)
    >>> setlocale(LC_CTYPE, "");
    >>> wchar_t a = L'한';
    >>> putwchar(a);
    >>> }
    >>>
    >>> result
    >>>
    >>> test.c:7: warning: multi-character character constant

    >>
    >> It works for me (as I'd expect). I image that the particular multi-byte
    >> sequence you have is not being recognised by the compiler.

    >
    > So you mean I should switch my tcc compiler to better compiler?


    Not necessarily. There may be compiler options that will make it work
    or, as Keith has suggested, you may be using an encoding that is not
    recognised by tcc. It might even as a simple as setting an environment
    variable during the compile.

    However, tcc is a small project and multi-byte support may well have
    been something that's been left to one side. There is a good mailing
    tcc list: .

    --
    Ben.
     
    Ben Bacarisse, Feb 10, 2011
    #5
  6. Owner

    Owner Guest

    On Thu, 10 Feb 2011 08:50:10 -0800, Keith Thompson wrote:

    > Owner <> writes:
    >> what's wrong with this code?
    >>
    >> #include <locale.h>
    >> #include <stdio.h>
    >> #include <wchar.h>
    >>
    >> main(){

    >
    > Should be "int main(void) {", but that's not the problem.
    >
    >> setlocale(LC_CTYPE, "");
    >> wchar_t a = L'ÇÑ';
    >> putwchar(a);
    >>
    >> }
    >>
    >> result
    >>
    >> test.c:7: warning: multi-character character constant

    >
    > It's hard to tell. Your article appears to have an euc-kr encoding
    > (judging by the "Content-Type:" header on your article), but
    > that could have been introduced by some news software somewhere.
    > Is your original source file encoded in euc-kr?


    yes, korean locale
    >
    > Probably your compiler doesn't recognize the character encoding you
    > used in your source file, and is interpreting the bytes between the
    > single quotes as two 8-bit characters, producing a multi-character
    > character constant.
    >
    > Note that a call to setlocale() (which occurs at run time) cannot
    > affect the compiler's interpretation of source characters.
    >
    > See what your compiler's documentation says about source character
    > sets. You might hae better luck using UTF-8.
     
    Owner, Feb 10, 2011
    #6
  7. Owner

    Owner Guest

    On Thu, 10 Feb 2011 07:21:54 -0500, Owner wrote:

    > what's wrong with this code?
    >
    > #include <locale.h>
    > #include <stdio.h>
    > #include <wchar.h>
    >
    > main(){
    > setlocale(LC_CTYPE, "");
    > wchar_t a = L'ÇÑ';
    > putwchar(a);
    >
    > }
    >
    > result
    >
    > test.c:7: warning: multi-character character constant


    Strangely this below code works then.

    #include <locale.h>
    #include <stdio.h>
    #include <wchar.h>


    main(){
    wchar_t *a = L"ÇѱÛ";
    setlocale(LC_ALL, "ko");

    wprintf(L"%s",a);

    }
     
    Owner, Feb 10, 2011
    #7
  8. Owner <> writes:
    > On Thu, 10 Feb 2011 07:21:54 -0500, Owner wrote:
    >> what's wrong with this code?
    >>
    >> #include <locale.h>
    >> #include <stdio.h>
    >> #include <wchar.h>
    >>
    >> main(){
    >> setlocale(LC_CTYPE, "");
    >> wchar_t a = L'한';
    >> putwchar(a);
    >>
    >> }
    >>
    >> result
    >>
    >> test.c:7: warning: multi-character character constant

    >
    > Strangely this below code works then.
    >
    > #include <locale.h>
    > #include <stdio.h>
    > #include <wchar.h>
    >
    >
    > main(){
    > wchar_t *a = L"한글";
    > setlocale(LC_ALL, "ko");
    >
    > wprintf(L"%s",a);
    >
    > }


    Well, at least the compiler isn't going to warn about the second
    case; you're using a string literal rather than a character constant,
    so you can have as many characters/bytes/glyphs between them as
    you like.

    What does wcslen(a) return?

    (Note that, in my newsreader, your string literal looks something like
    L"[][]"
    only fuzzier.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 10, 2011
    #8
  9. Owner

    Owner Guest

    On Thu, 10 Feb 2011 15:52:49 -0800, Keith Thompson wrote:

    > Owner <> writes:
    >> On Thu, 10 Feb 2011 07:21:54 -0500, Owner wrote:
    >>> what's wrong with this code?
    >>>
    >>> #include <locale.h>
    >>> #include <stdio.h>
    >>> #include <wchar.h>
    >>>
    >>> main(){
    >>> setlocale(LC_CTYPE, "");
    >>> wchar_t a = L'ÇÑ';
    >>> putwchar(a);
    >>>
    >>> }
    >>>
    >>> result
    >>>
    >>> test.c:7: warning: multi-character character constant

    >>
    >> Strangely this below code works then.
    >>
    >> #include <locale.h>
    >> #include <stdio.h>
    >> #include <wchar.h>
    >>
    >>
    >> main(){
    >> wchar_t *a = L"ÇѱÛ";
    >> setlocale(LC_ALL, "ko");
    >>
    >> wprintf(L"%s",a);
    >>
    >> }

    >
    > Well, at least the compiler isn't going to warn about the second
    > case; you're using a string literal rather than a character constant,
    > so you can have as many characters/bytes/glyphs between them as
    > you like.
    >
    > What does wcslen(a) return?
    >
    > (Note that, in my newsreader, your string literal looks something like
    > L"[][]"
    > only fuzzier.)


    Solved!

    downloaded visual c++ express 2010 and compiled below code
    with cl.exe and worked.

    #include <locale.h>
    #include <stdio.h>
    #include <wchar.h>


    main(){
    wchar_t a = L'ÇÑ';
    setlocale(LC_ALL, "kor");

    putwchar(a);

    }
     
    Owner, Feb 11, 2011
    #9
  10. Owner

    Owner Guest

    On Thu, 10 Feb 2011 19:37:57 -0500, Owner wrote:

    > On Thu, 10 Feb 2011 15:52:49 -0800, Keith Thompson wrote:
    >
    >> Owner <> writes:
    >>> On Thu, 10 Feb 2011 07:21:54 -0500, Owner wrote:
    >>>> what's wrong with this code?
    >>>>
    >>>> #include <locale.h>
    >>>> #include <stdio.h>
    >>>> #include <wchar.h>
    >>>>
    >>>> main(){
    >>>> setlocale(LC_CTYPE, "");
    >>>> wchar_t a = L'ÇÑ';
    >>>> putwchar(a);
    >>>>
    >>>> }
    >>>>
    >>>> result
    >>>>
    >>>> test.c:7: warning: multi-character character constant
    >>>
    >>> Strangely this below code works then.
    >>>
    >>> #include <locale.h>
    >>> #include <stdio.h>
    >>> #include <wchar.h>
    >>>
    >>>
    >>> main(){
    >>> wchar_t *a = L"ÇѱÛ";
    >>> setlocale(LC_ALL, "ko");
    >>>
    >>> wprintf(L"%s",a);
    >>>
    >>> }

    >>
    >> Well, at least the compiler isn't going to warn about the second
    >> case; you're using a string literal rather than a character constant,
    >> so you can have as many characters/bytes/glyphs between them as
    >> you like.
    >>
    >> What does wcslen(a) return?


    4


    >>
    >> (Note that, in my newsreader, your string literal looks something like
    >> L"[][]"
    >> only fuzzier.)


    supposed to be korean in 2 letters

    >
    > Solved!
    >
    > downloaded visual c++ express 2010 and compiled below code
    > with cl.exe and worked.
    >
    > #include <locale.h>
    > #include <stdio.h>
    > #include <wchar.h>
    >
    >
    > main(){
    > wchar_t a = L'ÇÑ';
    > setlocale(LC_ALL, "kor");
    >
    > putwchar(a);
    >
    > }
     
    Owner, Feb 11, 2011
    #10
  11. Owner

    Owner Guest


    >>>
    >>> What does wcslen(a) return?

    >
    > 4
    >
    >

    and visual c++ express 2010 cl.exe returns 2 which is correct
    length of the string.
     
    Owner, Feb 11, 2011
    #11
    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. Maurice Hulsman
    Replies:
    1
    Views:
    1,849
    Guus Bosman
    Jul 25, 2004
  2. Replies:
    3
    Views:
    1,105
    James Kanze
    Aug 15, 2008
  3. Replies:
    4
    Views:
    1,003
  4. Replies:
    1
    Views:
    382
    Codeplug
    Mar 5, 2009
  5. zade
    Replies:
    1
    Views:
    605
    James Kanze
    Mar 5, 2010
Loading...

Share This Page