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) -1is returned. ๐ dsizenon-L'\0'wide characters have been stored atdest. in this case, the number of wide characters written todestis 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 todest, 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
- Copyright 2014, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms