
- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <float.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdarg.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <time.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C library function - wctomb()
Description
The C library function int wctomb(char *str, wchar_t wchar) function converts the wide character wchar to its multibyte representation and stores it at the beginning of the character array pointed to by str.
Declaration
Following is the declaration for wctomb() function.
int wctomb(char *str, wchar_t wchar)
Parameters
str − This is the pointer to an array large enough to hold a multibyte character,
wchar − This is the wide character of type wchar_t.
Return Value
If str is not NULL, the wctomb() function returns the number of bytes that have been written to the byte array at str. If wchar cannot be represented as a multibyte sequence, -1 is returned.
If str is NULL, the wctomb() function returns non-zero if the encoding has non-trivial shift state, or zero if the encoding is stateless.
Example
The following example shows the usage of wctomb() function.
#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); }
Let us compile and run the above program that will produce the following result −
Converting wide character: Characters converted: 1 Multibyte character: a Trying to convert when target is NULL: Characters converted: 0 Multibyte character: