mbsrtowcs converts a multibyte string (MBS) into a null-terminated wide character string (WCS). Each wchar_t represents one character of the extended character set, while mbstate_t preserves conversion state across calls. section 14.3
size_t mbsrtowcs(wchar_t*restrict dst, char const**restrict src,
size_t len, mbstate_t*restrict ps);
The declaration exposes the conversion interface: destination storage, a source pointer that can advance, a destination limit, and conversion state. section 14.3
Counting converted characters
mbsrlen uses mbsrtowcs(nullptr, &s, 0, &st) to determine the required wide-character length without writing a destination string. It copies or selects conversion state first, then treats an invalid source as an error condition. section 14.3
Allocating a converted string
mbsrdup first obtains the converted length, allocates space for mblen + 1 wchar_t elements, and calls mbsrtowcs to produce the wide string when allocation succeeds.
TAKEAWAY:
- Preserve compatible mbstate_t state when conversion may continue across calls. section 14.3
- Check for invalid source data; the conversion may set errno. section 14.3

