1MBSTOWCS(3)                Linux Programmer's Manual               MBSTOWCS(3)
2
3
4

NAME

6       mbstowcs - convert a multibyte string to a wide-character string
7

SYNOPSIS

9       #include <stdlib.h>
10
11       size_t mbstowcs(wchar_t *dest, const char *src, size_t n);
12

DESCRIPTION

14       If  dest  is  not  NULL, the mbstowcs() function converts the multibyte
15       string src to a wide-character string starting at dest.  At most n wide
16       characters  are  written  to  dest.   The sequence of characters in the
17       string src shall begin in the initial shift state.  The conversion  can
18       stop for three reasons:
19
20       1. An  invalid  multibyte sequence has been encountered.  In this case,
21          (size_t) -1 is returned.
22
23       2. n non-L'\0' wide characters have been stored at dest.  In this case,
24          the  number  of wide characters written to dest is returned, but the
25          shift state at this point is lost.
26
27       3. The multibyte string has been completely  converted,  including  the
28          terminating null character ('\0').  In this case, the number of wide
29          characters written to dest,  excluding  the  terminating  null  wide
30          character, is returned.
31
32       The programmer must ensure that there is room for at least n wide char‐
33       acters at dest.
34
35       If dest is NULL, n is ignored, and the conversion  proceeds  as  above,
36       except  that  the converted wide characters are not written out to mem‐
37       ory, and that no length limit exists.
38
39       In order to avoid the case 2 above, the programmer should make  sure  n
40       is greater than or equal to mbstowcs(NULL,src,0)+1.
41

RETURN VALUE

43       The mbstowcs() function returns the number of wide characters that make
44       up the converted part of the wide-character string, not  including  the
45       terminating  null wide character.  If an invalid multibyte sequence was
46       encountered, (size_t) -1 is returned.
47

ATTRIBUTES

49       For  an  explanation  of  the  terms  used   in   this   section,   see
50       attributes(7).
51
52       ┌───────────┬───────────────┬─────────┐
53Interface  Attribute     Value   
54       ├───────────┼───────────────┼─────────┤
55mbstowcs() │ Thread safety │ MT-Safe │
56       └───────────┴───────────────┴─────────┘

CONFORMING TO

58       POSIX.1-2001, POSIX.1-2008, C99.
59

NOTES

61       The behavior of mbstowcs() depends on the LC_CTYPE category of the cur‐
62       rent locale.
63
64       The function mbsrtowcs(3) provides a better interface to the same func‐
65       tionality.
66

EXAMPLES

68       The program below illustrates the use of mbstowcs(), as well as some of
69       the wide character classification functions.  An  example  run  is  the
70       following:
71
72           $ ./t_mbstowcs de_DE.UTF-8 Grüße!
73           Length of source string (excluding terminator):
74               8 bytes
75               6 multibyte characters
76
77           Wide character string is: Grüße! (6 characters)
78               G alpha upper
79               r alpha lower
80               ü alpha lower
81               ß alpha lower
82               e alpha lower
83               ! !alpha
84
85   Program source
86
87       #include <wctype.h>
88       #include <locale.h>
89       #include <wchar.h>
90       #include <stdio.h>
91       #include <string.h>
92       #include <stdlib.h>
93
94       int
95       main(int argc, char *argv[])
96       {
97           size_t mbslen;      /* Number of multibyte characters in source */
98           wchar_t *wcs;       /* Pointer to converted wide character string */
99           wchar_t *wp;
100
101           if (argc < 3) {
102               fprintf(stderr, "Usage: %s <locale> <string>\n", argv[0]);
103               exit(EXIT_FAILURE);
104           }
105
106           /* Apply the specified locale */
107
108           if (setlocale(LC_ALL, argv[1]) == NULL) {
109               perror("setlocale");
110               exit(EXIT_FAILURE);
111           }
112
113           /* Calculate the length required to hold argv[2] converted to
114              a wide character string */
115
116           mbslen = mbstowcs(NULL, argv[2], 0);
117           if (mbslen == (size_t) -1) {
118               perror("mbstowcs");
119               exit(EXIT_FAILURE);
120           }
121
122           /* Describe the source string to the user */
123
124           printf("Length of source string (excluding terminator):\n");
125           printf("    %zu bytes\n", strlen(argv[2]));
126           printf("    %zu multibyte characters\n\n", mbslen);
127
128           /* Allocate wide character string of the desired size.  Add 1
129              to allow for terminating null wide character (L'\0'). */
130
131           wcs = calloc(mbslen + 1, sizeof(wchar_t));
132           if (wcs == NULL) {
133               perror("calloc");
134               exit(EXIT_FAILURE);
135           }
136
137           /* Convert the multibyte character string in argv[2] to a
138              wide character string */
139
140           if (mbstowcs(wcs, argv[2], mbslen + 1) == (size_t) -1) {
141               perror("mbstowcs");
142               exit(EXIT_FAILURE);
143           }
144
145           printf("Wide character string is: %ls (%zu characters)\n",
146                   wcs, mbslen);
147
148           /* Now do some inspection of the classes of the characters in
149              the wide character string */
150
151           for (wp = wcs; *wp != 0; wp++) {
152               printf("    %lc ", (wint_t) *wp);
153
154               if (!iswalpha(*wp))
155                   printf("!");
156               printf("alpha ");
157
158               if (iswalpha(*wp)) {
159                   if (iswupper(*wp))
160                       printf("upper ");
161
162                   if (iswlower(*wp))
163                       printf("lower ");
164               }
165
166               putchar('\n');
167           }
168
169           exit(EXIT_SUCCESS);
170       }
171

SEE ALSO

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

COLOPHON

176       This  page  is  part of release 5.07 of the Linux man-pages project.  A
177       description of the project, information about reporting bugs,  and  the
178       latest     version     of     this    page,    can    be    found    at
179       https://www.kernel.org/doc/man-pages/.
180
181
182
183GNU                               2020-06-09                       MBSTOWCS(3)
Impressum