wctomb() - C語言庫函數

C庫函數 int wctomb(char *str, wchar_t wchar) 函數將寬字元wchar 多位元組表示,並把它存儲在字元數組指向 bystr 的開始。

聲明

以下是wctomb() 函數的聲明。

int wctomb(char *str, wchar_t wchar)

參數

  • str -- 這是大到足以容納一個多位元組字元數組的指針,

  • wchar -- 這是寬字元wchar_t類型。

返回值

  • 如果str是不是NULL,wctomb() 函數返回已寫入位元組數組 str 位元組數。如果wchar 不能表示為多位元組序列,則返回-1。

  • 如果str是NULL,wctomb() 函數返回非零如果編碼非平凡的轉變狀態,或者為零,如果編碼是無狀態的。

例子

下麵的例子顯示 wctomb() 函數的用法。

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

int main()
{
   int i;
   wchar_t wc = L'a';
   char *pmbnull = NULL;
   char *pmb = (char *)malloc(sizeof( char ));

   printf("Converting wide character:
");
   i = wctomb( pmb, wc );
   printf("Characters converted: %u
", i);
   printf("Multibyte character: %.1s
", pmb);

   printf("Trying to convert when target is NULL:
");
   i = wctomb( pmbnull, wc );
   printf("Characters converted: %u
", i);
   /* this will not print any value */
   printf("Multibyte character: %.1s
", pmbnull);

   return(0);
}

讓我們編譯和運行上面的程式,這將產生以下結果:

Converting wide character:
Characters converted: 1
Multibyte character: a
Trying to convert when target is NULL:
Characters converted: 0
Multibyte character: 

上一篇: wcstombs() - C語言庫函數 下一篇: <string.h> - C語言標準庫