char8_t

来自cppreference.com
< c‎ | string‎ | multibyte
在标头 <uchar.h> 定义
typedef unsigned char char8_t;
(C23 起)

char8_t 为用于 UTF-8 的无符号整数类型,且与 unsigned char 为相同类型。

示例

#include <uchar.h>
#include <stdio.h>
 
int main(void)
{
    char8_t str[] = u8"zß水🍌"; // 或 "z\u00df\u6c34\U0001f34c"
    size_t str_sz = sizeof str; // sizeof *str == 1 by definition
    printf("%zu UTF-8 code units: [ ", str_sz);
    for (size_t n = 0; n < str_sz; ++n)
        printf("%02X ", str[n]);
    printf("]\n");
}

可能的输出:

11 UTF-8 code units: [ 7A C3 9F E6 B0 B4 F0 9F 8D 8C 00 ]

引用

  • C23 标准(ISO/IEC 9899:2024):
  • 7.30 Unicode utilities <uchar.h> (第 410 页)

参阅