dthusian
dthusian

Reputation: 378

C represent a UTF-8 multichar

I am trying to store the block character (█, U+2588) in a char variable.

#include "stdio.h"

int main(void) {
  char block = '█';
  printf("Without storage: █\n");
  printf("With storage: %c\n", block);
}

But it gives me the warning

main.c: In function 'main':
main.c:4:20: warning: multi-character character constant [-Wmultichar]
       char block = '█';
                    ^~~~~
main.c:4:20: warning: overflow in implicit constant conversion [-Woverflow]

The code outputs:

Without storage: █
With storage: �

The 'with storage' unknown is probably due to the char overflowing and only representing half of the character.

I am aware that the block character could be a UTF-8 multichar, so how might someone represent a UTF-8 multichar without any warnings?

Upvotes: 1

Views: 323

Answers (1)

deamentiaemundi
deamentiaemundi

Reputation: 5525

That is a wide character, hence a need for a different type. In this case wchar_t is the better choice, but that alone will not do it, you need to mark the constant itself as a wide character, too. Example:

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main()
{
   char c='A';
   wchar_t wc=L'█';

   printf("d%lcd d█d\n",wc); 
   puts("end");

   exit(EXIT_SUCCESS);
}

The marking for a wide charater constant is L (either single character or string) and to print it %lc and %ls respectively. But that alone didn't do it for me, the second printf still printed nothing. It was a problem of the console itself and setting locale helped (at least for me)

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <locale.h>
int main()
{
   char c='A';
   wchar_t wc=L'█';
   setlocale(LC_CTYPE, "");
   printf("%c d█d\n",c);
   printf("d%lcd d█d\n",wc); 
   puts("end");

   exit(EXIT_SUCCESS);
}

Upvotes: 1

Related Questions