How to use the new char16_t and u8 types?

2.2k views Asked by At

c11 improve encoding support with built-in types for utf-8 utf-16 and utf-32.

However I was completely unable to find reference on using them in Standard functions. All I found is how to use them in c++11 not in C.

So how to printf a char32_t for example?

1

There are 1 answers

4
一二三 On

There isn't much to say: C11 only introduced four new standard library functions for working with char16_t and char32_t, which convert them to/from multibyte strings:

With respect to printf(), they behave like uint_least16_t and uint_least32_t types, so you can use the same format specifiers for them:

#include <inttypes.h>

char32_t x = ...;
printf("%" PRIuLEAST32 "\n", x);

If you want to print the value as a character, you will need to use the conversion functions above.

Working with char16_t and char32_t character and string literals is identical in both C11 and C++11.