locale and wchar_t character constant

O

Owner

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
 
B

Ben Bacarisse

Owner said:
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.
 
O

Owner

int main(void)


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?
 
K

Keith Thompson

Owner said:
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.
 
B

Ben Bacarisse

Owner said:
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: (e-mail address removed).
 
O

Owner

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


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
 
O

Owner

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);

}
 
K

Keith Thompson

Owner said:
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.)
 
O

Owner

Owner said:
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);

}
 
O

Owner

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
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top