Certainly, my problem is not new…., so I apologize if my error is simply too stupid.
I just wanted to become familiar with putwchar
and simply wrote the following little piece of code:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(void)
{
char *locale = setlocale(LC_ALL, "");
printf ("Locale: %sn", locale);
//setlocale(LC_CTYPE, "de_DE.utf8");
wchar_t hello[]=L"Registered Trademark: ®®nEuro sign: €€nBritisch Pound: ££nYen: ¥¥nGerman Umlauts: äöüßÄÖÜn";
int index = 0;
while (hello[index]!=L''){
//printf("put liefert: %dn", putwchar(hello[index++]));
putwchar(hello[index++]);
};
}
Now. the output is simply:
Locale: de_DE.UTF-8
Registered Trademark: ��
Euro sign: ��
Britisch Pound: ��
Yen: ��
German Umlauts: �������
[1]+ Fertig gedit versuch.c
None of the non-ASCII chars appeared on the screen.
As you see in the comment (and I well noticed that I must not mix putwchar
and print in the same program, hence the line is in comment, putwchar
returned the proper Unicode codepoint for the character I wanted to print. Thus, the call is supposed to work. (At least to my understanding.)
The c source is coded in utf-8
$ file versuch.c
versuch.c: C source, UTF-8 Unicode text
my system is Ubuntu Linux 20.04.05
compiler: gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
I would greatly appreciate any advice on this one.
As stated above: I simply expected the trademark sign, yen, € and the umlauts äöüßÄÖÜ to appear.
3
Answers
You can simply print those wide characters as shown below:
Please refer:
You cannot mix narrow and wide I/O in the same stream (7.21.2). If you want
putwchar
, you cannot useprintf
. Start withwprintf
instead (with the wide format string):You shouldn’t mix normal and wide output on the same stream.
I get the expected output if I change this early print:
into a wide print:
Then the subsequent
putwchar()
calls write the expected characters.