简体   繁体   中英

output a unicode character in c language

I tried to output an airplane unicode character in the C language, but I got nothing on the console.

Here is my code:

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

int main() {
    setlocale(LC_CTYPE, setlocale(LC_ALL, ""));
    wprintf(L"🛧");

    return 0;
}

ignore the extra headers, which serve for other purposes.

I expectedto be shown on the console, but nothing appeared. I have tried to output some other languages in wprint such as Japanese and Chinese: those just worked fine. But many other glyphs like airplanes in other shapes and most of the arrows can't work, either. just nothing is printed.

The first issue is printing the correct character. L"" denotes a wide character string. Whether that is 16bit wide (Windows) or 32bit wide (Linux) is implementation-defined. Whether it is Unicode-encoded is locale-dependent.

Whether your compiler supports Unicode characters in source is (AFAIK) still implementation-defined.

Whether your output medium (console in your case) supports wide-character / Unicode strings is depending on that output medium.

Whether the font you configured for that output medium supports the character(s) you want to print is depending on the font.

If you want to use Unicode specifically, you should use char8_t[] (UTF-8) or char32_t[] (UTF-32) -- char16_t / UTF-16 also exists if that is what your output medium supports -- and specify characters using \u.... or \U........ character escapes. That takes care of the first two paragraphs of my answer. The next two could explain why you still might not get what you expected.

Despite you lack a #include <wchar.h> header, your program works as espected.

Check if your console has utf support (normally this means capable of receiving utf8 encoding) If not, it's no worth trying to show an unicode output on it, for evident reasons.

In Linux, this requires that you have a locale (check it executing locale in your console) that includes UTF-8 suffix for utf support (depending on your operating system/version, you will need to spell it in uppercase or lowercase, depending on how the locales have been compiled in your platform)

If you are using Windows, I'm afraid the windows console has no Unicode support (or it has since short ago) so the problem is not in the program (as I say, works perfect) but in the output device.

Things you can still try:

  • set a locale listed in locale -a by executing the following sequence, then run the program to adjust a proper locale:
export LANG="es_ES.UTF-8"

(as I say, that locale must appear when you list all locales with the command above, and it must support unicode encodings)

  • ensure that your console supports the locale you are using.
  • redirect the output of your program to a file, and edit that file with a binary editor/viewer, to show the actual bytes output by the program. This will allow you to check if the program effectively printed nothing, or if the problem is that the output terminal doesn't hav a little plane to show (this can also be the problem) Not only suffices to say draw a plane , the receiver must know how to draw it.
  • Double check that you have included in your locale one that specifies to use unicode characters (whatever encoding is supported on your system) Even if you are in USA, you need a locale that includes Unicode support, to support non-ASCII characters.

On windows with Visual Studio 2022 this works for me:

#include <Windows.h>
#include <stdio.h>

int main()
{
    SetConsoleOutputCP(CP_UTF8);
    puts(u8"🛧😊");
}

Note the file neds to be saved using UTF-8 encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM