skip to Main Content

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


  1. You can simply print those wide characters as shown below:

    wprintf(L"Registered Trade Mark: %lsn", L"®®");
    wprintf(L"Euro Sign: %lsn", L"€€");
    wprintf(L"British Pound: %lsn", L"££");
    wprintf(L"Yen: %lsn", L"¥¥");
    wprintf(L"German Umlauts: %lsn", L"äöüßÄÖÜ");
    

    Please refer:

    Login or Signup to reply.
  2. You cannot mix narrow and wide I/O in the same stream (7.21.2). If you want putwchar, you cannot use printf. Start with wprintf instead (with the wide format string):

    wprintf (L"Locale: %sn", locale);
    
    Login or Signup to reply.
  3. You shouldn’t mix normal and wide output on the same stream.

    I get the expected output if I change this early print:

      printf ("Locale: %sn", locale);
    

    into a wide print:

        wprintf(L"Locale: %sn", locale);
    

    Then the subsequent putwchar() calls write the expected characters.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search