1) On success, returns the number of bytes (including any shift sequences, but excluding the terminating ' \0 ' ) written to the character array whose first element is pointed to by dst . If dst is a null pointer, returns the number of bytes that would have been written. On conversion error (if invalid wide character was encountered), returns ( size_t ) - 1 , stores EILSEQ in errno , and leaves * ps in unspecified state.
2) Returns zero on success (in which case the number of bytes excluding terminating zero that were, or would be written to dst , is stored in * retval ), non-zero on error. In case of a runtime constraint violation, stores ( size_t ) - 1 in * retval (unless retval is null) and sets dst [ 0 ] to ' \0 ' (unless dst is null or dstmax is zero or greater than RSIZE_MAX )
#include #include #include #include void print_wide(const wchar_t* wstr) { mbstate_t state; memset(&state, 0, sizeof state); size_t len = 1 + wcsrtombs(NULL, &wstr, 0, &state); char mbstr[len]; wcsrtombs(mbstr, &wstr, len, &state); printf("Multibyte string: %s\n", mbstr); printf("Length, including '\\0': %zu\n", len); } int main(void) { setlocale(LC_ALL, "en_US.utf8"); print_wide(L"z\u00df\u6c34\U0001f34c"); // or L"zß水🍌" }
Multibyte string: zß水🍌 Length, including '\0': 11