Search code examples
cunicodeescaping

how to use unicode blockelements in C?


I want to use unicode blocks in my C program to display them in the console like ▇, ░ and so on. However, whenever I try to use the escape sequence for unicode characters, I only get weird letters like:

printf("/u259A"); //259A is the unicode for ▚
Output: ÔûÜ

I looked up how to include unicode charactes then tried to use wchar_t:

#include <locale.h>
#include <stdio.h>
int main(int argc, char const *argv[]) {
  setlocale(LC_ALL, "");
  wchar_t c = "\u259A";
  printf("%c",c);
  return 0;
}

but that only gave me as the output instead of ▚. Removing setlocale() would give me a blank output. I dont know what do to from this point on. The only thing I saw was using printf("\xB2"); which gave you . But I dont understand where the B2 comes from or what it stands for.


Solution

  • So what worked for me was the following:

    #include <stdio.h>
    #include <fcntl.h>
    #include <io.h>
    
    int main(int argc, char const *argv[]) {
      _setmode(_fileno(stdout), _O_U16TEXT);
      wprintf(L"\x2590 \x2554 \x258c \x2592"); //Output : ▐ ╔ ▌ ▒
      return 0;
    }
    

    the function _setmode() is apparently for setting the console on u16 text encoding. wprintf() allows you to print wide characters (unicode aswell). The L"" before the string indicates to the compiler, that the following string is a unicode string. Thanks to everyone for their time and answers!