描述
C庫函數int wctomb(char *str, wchar_t wchar)函數轉換寬字符wchar到其多字節表示並將其存儲在指向的字符數組的開頭str。
聲明
以下是 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:\n");
i = wctomb( pmb, wc );
printf("Characters converted:%u\n", i);
printf("Multibyte character:%.1s\n", pmb);
printf("Trying to convert when target is NULL:\n");
i = wctomb( pmbnull, wc );
printf("Characters converted:%u\n", i);
/* this will not print any value */
printf("Multibyte character:%.1s\n", pmbnull);
return(0);
}
讓我們編譯並運行上麵的程序,將產生以下結果 -
Converting wide character: Characters converted:1 Multibyte character:a Trying to convert when target is NULL: Characters converted:0 Multibyte character:
相關用法
- C語言 wcstombs()用法及代碼示例
- C語言 宏 assert()用法及代碼示例
- C語言 vprintf()用法及代碼示例
- C語言 宏 va_start()用法及代碼示例
- C語言 setlocale()用法及代碼示例
- C語言 fread()用法及代碼示例
- C語言 sinh()用法及代碼示例
- C語言 宏 offsetof()用法及代碼示例
- C語言 feof()用法及代碼示例
- C語言 scanf()用法及代碼示例
- C語言 imagesize()用法及代碼示例
- C語言 getarcoords()用法及代碼示例
- C語言 isdigit()用法及代碼示例
- C語言 clock()用法及代碼示例
- C語言 strcspn()用法及代碼示例
- C語言 setlinestyle()用法及代碼示例
- C語言 fmod()用法及代碼示例
- C語言 showbits()用法及代碼示例
- C語言 div()用法及代碼示例
- C語言 outtextxy()用法及代碼示例
注:本文由純淨天空篩選整理自 C library function - wctomb()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。