mbstowcs(3)

๐Ÿ’– NAME ๐Ÿ’–

okay so bestie, we're talking about mbstowcs โ€” it converts a multibyte string to a wide-character string. that's literally the whole point. periodt. โœจ this one's from the Library Functions Manual, btw.

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc). it's giving classic. ๐Ÿซถ

โœจ SYNOPSIS โœจ

       #include <stdlib.h>

       size_t mbstowcs(size_t dsize;
                       wchar_t dest[restrict dsize], const char *restrict src,
                       size_t dsize);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so if dest is not NULL, we convert the multibyte string src to a wide-character string starting at dest. at most dsize wide characters get written to dest. the sequence of characters in the string src shall begin in the initial shift state. the conversion can stop for three reasons, and they're kinda important:

  • An invalid multibyte sequence has been encountered. in this case, (size_t) -1 is returned. ๐Ÿ’€
  • dsize non-L'\0' wide characters have been stored at dest. in this case, the number of wide characters written to dest is returned, but the shift state at this point is lost. ๐Ÿ˜ญ
  • The multibyte string has been completely converted, including the terminating null character ('\0'). in this case, the number of wide characters written to dest, excluding the terminating null wide character, is returned. slay. ๐Ÿ‘‘

if dest is NULL, dsize is ignored, and the conversion proceeds as above, except that the converted wide characters are not written out to memory, and that no length limit exists. iconic. ๐ŸŒˆ

in order to avoid the case 2 above, the programmer should make sure dsize is greater than or equal to mbstowcs(NULL,src,0)+1. no cap. ๐Ÿงš

the programmer must ensure that there is room for at least dsize wide characters at dest. like, actually. don't skip this. ๐Ÿ™ˆ

๐Ÿ’ซ RETURN VALUE ๐Ÿ’ซ

the number of wide characters that make up the converted part of the wide-character string, not including the terminating null wide character. if an invalid multibyte sequence was encountered, (size_t) -1 is returned. that's the tea. ๐Ÿ“

๐ŸŒธ ATTRIBUTES ๐ŸŒธ

for an explanation of the terms used in this section, see attributes(7).

  • Interface: mbstowcs() โ€” Attribute: Thread safety โ€” Value: MT-Safe

๐Ÿ’… VERSIONS ๐Ÿ’…

the function mbsrtowcs(3) provides a better interface to the same functionality. lowkey upgrade. โœจ

๐ŸŽ€ STANDARDS ๐ŸŽ€

C11, POSIX.1-2008. yes queen. ๐Ÿ‘‘

โญ HISTORY โญ

POSIX.1-2001, C99. old but gold. ๐Ÿ’œ

๐Ÿ”ฅ NOTES ๐Ÿ”ฅ

the behavior of mbstowcs() depends on the LC_CTYPE category of the current locale. iykyk. ๐ŸŒ

๐Ÿฆ„ EXAMPLES ๐Ÿฆ„

the program below illustrates the use of mbstowcs(), as well as some of the wide character classification functions. an example run is the following:

       $ ./t_mbstowcs de_DE.UTF-8 GrรผรŸe!
       Length of source string (excluding terminator):
           8 bytes
           6 multibyte characters

       Wide character string is: GrรผรŸe! (6 characters)
           G alpha upper
           r alpha lower
           รผ alpha lower
           รŸ alpha lower
           e alpha lower
           ! !alpha

๐ŸŽ€ Program source ๐ŸŽ€

       #include <locale.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <string.h>
       #include <wchar.h>
       #include <wctype.h>

       int
       main(int argc, char *argv[])
       {
           size_t mbslen;      /* Number of multibyte characters in source */
           wchar_t *wcs;       /* Pointer to converted wide character string */

           if (argc < 3) {
               fprintf(stderr, "Usage: %s <locale> <string>\n", argv[0]);
               exit(EXIT_FAILURE);
           }

           /* Apply the specified locale.  */

           if (setlocale(LC_ALL, argv[1]) == NULL) {
               perror("setlocale");
               exit(EXIT_FAILURE);
           }

           /* Calculate the length required to hold argv[2] converted to
              a wide character string.  */

           mbslen = mbstowcs(NULL, argv[2], 0);
           if (mbslen == (size_t) -1) {
               perror("mbstowcs");
               exit(EXIT_FAILURE);
           }

           /* Describe the source string to the user.  */

           printf("Length of source string (excluding terminator):\n");
           printf("    %zu bytes\n", strlen(argv[2]));
           printf("    %zu multibyte characters\n\n", mbslen);

           /* Allocate wide character string of the desired size.  Add 1
              to allow for terminating null wide character (L'\0').  */

           wcs = calloc(mbslen + 1, sizeof(*wcs));
           if (wcs == NULL) {
               perror("calloc");
               exit(EXIT_FAILURE);
           }

           /* Convert the multibyte character string in argv[2] to a
              wide character string.  */

           if (mbstowcs(wcs, argv[2], mbslen + 1) == (size_t) -1) {
               perror("mbstowcs");
               exit(EXIT_FAILURE);
           }

           printf("Wide character string is: %ls (%zu characters)\n",
                  wcs, mbslen);

           /* Now do some inspection of the classes of the characters in
              the wide character string.  */

           for (wchar_t *wp = wcs; *wp != 0; wp++) {
               printf("    %lc ", (wint_t) *wp);

               if (!iswalpha(*wp))
                   printf("!");
               printf("alpha ");

               if (iswalpha(*wp)) {
                   if (iswupper(*wp))
                       printf("upper ");

                   if (iswlower(*wp))
                       printf("lower ");
               }

               putchar('\n');
           }

           exit(EXIT_SUCCESS);
       }

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

mblen(3), mbsrtowcs(3), mbtowc(3), wcstombs(3), wctomb(3)


okay bestie, that's the whole thing! this page came from the Library Functions Manual, and it's mbstowcs(3) in the Linux man-pages 6.20, dated 2026-02-08. mwah! ๐Ÿ’‹โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: GPL-2.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms