Why are Unicode characters not displayed correctly in a terminal with GCC?


Samik

I wrote a small C program:

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

int main() {
    wprintf(L"%s\n", setlocale(LC_ALL, "C.UTF-8"));
    wchar_t chr = L'┐';
    wprintf(L"%c\n", chr);
}

Why doesn't this print characters ?

Instead it prints out garbled characters.unicode test

I checked:

  • Tried compiling without setlocale, same result
  • The terminal itself can print characters, and I can copy-paste it into the terminal from a text editor, this is gnome-terminal on Ubuntu
  • GCC version is 4.8.2
Conor Smith

wprintfis a version that printfuses a wide string as its format string, but otherwise behaves the same: %cstill treated as char, not wchar_t. Therefore, you need to use %lcformatted wide characters. And since your string is ASCII, you can use that too printf. E.g:

int main() {
    printf("%s\n", setlocale(LC_ALL, "C.UTF-8"));
    wchar_t chr = L'┐';
    printf("%lc\n", chr);
}

Related


Detect when Unicode characters cannot be displayed correctly

Jordan H Some unicode characters do not display on iOS, but display correctly on macOS. Likewise, some Unicode characters that iOS can display cannot be displayed on watchOS. This is due to the different built-in fonts installed on these platforms. If the char

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Why is my Applicaion not displaying Unicode characters correctly?

WonFeiHong I decided to convert my win32 c++ application to Unicode version, but when I use it I get unreadable letters in Arabic, Chinese and Japanese... First: If I don't use Unicode, I can find the name window title in Arabic in the edit box: HWND hWnd = Cr

Unicode characters are not displayed correctly in JTextArea

shoe I'm writing a text editor that needs to display and allow versions of Unicode characters. I'm developing it in Jython as the data model is in Python and the GUI will be using the Java Swing library. When I load one of these files containing Unicode charac

Unicode characters are not displayed correctly in JTextArea

shoe I'm writing a text editor that needs to display and allow versions of Unicode characters. I'm developing it in Jython as the data model is in Python and the GUI will be using the Java Swing library. When I load one of these files containing Unicode charac

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Detect when Unicode characters cannot be displayed correctly

Jordan H Some unicode characters do not display on iOS, but display correctly on macOS. Likewise, some Unicode characters that iOS can display cannot be displayed on watchOS. This is due to the different built-in fonts installed on these platforms. If the char

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Why is my Applicaion not displaying Unicode characters correctly?

WonFeiHong I decided to convert my win32 c++ application to Unicode version, but when I use it I get unreadable letters in Arabic, Chinese and Japanese... First: If I don't use Unicode, I can find the name window title in Arabic in the edit box: HWND hWnd = Cr

Why is my Applicaion not displaying Unicode characters correctly?

WonFeiHong I decided to convert my win32 c++ application to Unicode version, but when I use it I get unreadable letters in Arabic, Chinese and Japanese... First: If I don't use Unicode, I can find the name window title in Arabic in the edit box: HWND hWnd = Cr

Turkish characters are not displayed correctly

Boopathi Rajan I have the following code. It contains Turkish content. But the result I get includes special characters. So please provide a solution for this. HTML code <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=u

Unicode characters are not displayed correctly in JTextArea

shoe I'm writing a text editor that needs to display and allow versions of Unicode characters. I'm developing it in Jython as the data model is in Python and the GUI will be using the Java Swing library. When I load one of these files containing Unicode charac

Unicode characters are not displayed correctly in JTextArea

shoe I'm writing a text editor that needs to display and allow versions of Unicode characters. I'm developing it in Jython because the data model is written in Python and the GUI will use the Java Swing library. When I load one of these files containing Unicod

Detect when Unicode characters cannot be displayed correctly

Jordan H Some unicode characters do not display on iOS, but display correctly on macOS. Likewise, some Unicode characters that iOS can display cannot be displayed on watchOS. This is due to the different built-in fonts installed on these platforms. If the char

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Unicode characters not printing correctly in terminal python

Gulib 94 I wrote a very simple program that tells me the unicode value of certain characters. Here is the program: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character +

Why is my Applicaion not displaying Unicode characters correctly?

WonFeiHong I decided to convert my win32 c++ application to Unicode version, but when I use it I get unreadable letters in Arabic, Chinese and Japanese... First: If I don't use Unicode, I can find the name window title in Arabic in the edit box: HWND hWnd = Cr

Why is my Applicaion not displaying Unicode characters correctly?

WonFeiHong I decided to convert my win32 c++ application to Unicode version, but when I use it I get unreadable letters in Arabic, Chinese and Japanese... First: If I don't use Unicode, I can find the name window title in Arabic in the edit box: HWND hWnd = Cr

Why are Unicode characters not rendering correctly

sw1nn background: I have some unicode characters in my prompt (essentially git status tags) I'm running urxvt under xfce on Arch Linux. I'm using DejaVu Sans Mono as the Powerline font specified via the .Xresources line: URxvt*font: xft:DejaVu Sans Mono for Po

Turkish characters are not displayed correctly

Boopathi Rajan I have the following code. It contains Turkish content. But the result I get includes special characters. So please provide a solution for this. HTML code <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=u

Unicode characters are not displayed correctly in JTextArea

shoe I'm writing a text editor that needs to display and allow versions of Unicode characters. I'm developing it in Jython because the data model is written in Python and the GUI will use the Java Swing library. When I load one of these files containing Unicod