Ben Bacarisse wrote:
>
You need "%ls". This is very important with wprintf since without it
%s denotes a multi-byte character sequence. printf("%ls\n" input)
should also work. You need the w version if you want the multi-byte
conversion of %s or if the format has to be a wchar_t pointer.
Perhaps you may help me understand better. We have the usual char
encoding which is implementation defined (usually ASCII).
wchar_t is wide character encoding, which is the "largest character set
supported by the system", so I suppose Unicode under Linux and Windows.
What exactly is a multi-byte character?
I have to say that I am talking about C95 here, not C99.
>
> return 0;
}
Under Linux:
[john@localhost src]$ ./foobar-cpp
Test
T
[john@localhost src]$
[john@localhost src]$ ./foobar-cpp
Δοκιμαστικό
�
[john@localhost src]$
The above my not be the only problem. In cases like this, you need to
say way encoding your terminal is using.
You are somehow correct on this. My terminal encoding was UTF-8 and I
added Greek(ISO-8859-7). Under the last, the following code works OK:
#include <wchar.h>
#include <locale.h>
#include <stdio.h>
#include <stddef.h>
int main()
{
char *p= setlocale( LC_ALL, "Greek" );
wprintf(L"Δοκιμαστικό\n");
return 0;
}
[john@localhost src]$ ./foobar-cpp
Δοκιμαστικό
[john@localhost src]$
Also the original, fixed according to your suggestion:
#include <wchar.h>
#include <locale.h>
#include <stdio.h>
#include <stddef.h>
int main()
{
char *p= setlocale( LC_ALL, "Greek" );
wchar_t input[50];
if (!p)
printf("NULL returned!\n");
fgetws(input, 50, stdin);
wprintf(L"%ls", input);
return 0;
}
works OK too:
[john@localhost src]$ ./foobar-cpp
Δοκιμαστικό
Δοκιμαστικό
[john@localhost src]$
It works OK under Terminal UTF-8 default encoding too. So "%ls" is what
was really needed.
BTW, how can we define UTF-8 as the locale?
Thanks a lot.