Latest web development tutorials

C library functions - mbtowc ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

C library functionsint mbtowc (whcar_t * pwc, const char * str, size_t n) to a multi-byte sequence is converted to a wide character.

statement

Here is () statement mbtowc function.

int mbtowc(whcar_t *pwc, const char *str, size_t n)

parameter

  • pwc - point type wchar_t pointer to the object.
  • str - points to the first byte of multi-byte character pointer.
  • n - Maximum number of bytes to be checked.

return value

  • If str is not NULL, mbtowc () function returns the number of bytes consumed str beginning, if you point to a null byte, 0 is returned if the operation fails, it returns -1.
  • If str is NULL, if the encoder has a shift state, mbtowc () function returns nonzero if the encoding is stateless, it returns zero.

Examples

The following example demonstrates mbtowc () function is used.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   char *str = "这里是 w3cschool.cc";
   wchar_t mb[100];
   int len;
   
   len = mblen(NULL, MB_CUR_MAX); 

   mbtowc(mb, str, len*strlen(str) );
   
   wprintf(L"%ls \n", mb );   
   
   return(0);
}

Let's compile and run the above program, which will result in the following, because it needs to output multi-byte form, which is a binary output.

???

C standard library - <stdlib.h> C standard library - <stdlib.h>