1mbstowcs(3)                Library Functions Manual                mbstowcs(3)
2
3
4

NAME

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

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <stdlib.h>
13
14       size_t mbstowcs(wchar_t dest[restrict .n], const char *restrict src,
15                       size_t n);
16

DESCRIPTION

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

RETURN VALUE

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

ATTRIBUTES

53       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
54       tributes(7).
55
56       ┌────────────────────────────────────────────┬───────────────┬─────────┐
57Interface                                   Attribute     Value   
58       ├────────────────────────────────────────────┼───────────────┼─────────┤
59mbstowcs()                                  │ Thread safety │ MT-Safe │
60       └────────────────────────────────────────────┴───────────────┴─────────┘
61

VERSIONS

63       The function mbsrtowcs(3) provides a better interface to the same func‐
64       tionality.
65

STANDARDS

67       C11, POSIX.1-2008.
68

HISTORY

70       POSIX.1-2001, C99.
71

NOTES

73       The behavior of mbstowcs() depends on the LC_CTYPE category of the cur‐
74       rent locale.
75

EXAMPLES

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

SEE ALSO

181       mblen(3), mbsrtowcs(3), mbtowc(3), wcstombs(3), wctomb(3)
182
183
184
185Linux man-pages 6.04              2023-03-30                       mbstowcs(3)
Impressum