2
0

mbsrtowcs.c 577 B

123456789101112131415161718192021222324
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <wchar.h>
  4. void print_as_wide(const char* mbstr)
  5. {
  6. mbstate_t state;
  7. memset(&state, 0, sizeof state);
  8. size_t len = 1 + mbsrtowcs(NULL, &mbstr, 0, &state);
  9. wchar_t wstr[len];
  10. mbsrtowcs(&wstr[0], &mbstr, len, &state);
  11. //Should be 5
  12. printf("The length, including '\\0': %li \n",len);
  13. //missing wprintf to print this wide string
  14. //wprintf(L"The wide string: %ls \n", &wstr[0]);
  15. }
  16. int main()
  17. {
  18. const char* mbstr = u8"z\u00df\u6c34\U0001f34c"; // or u8"zß水🍌"
  19. print_as_wide(mbstr);
  20. }