
- 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 - wcstombs()
Description
The C library function size_t wcstombs(char *str, const wchar_t *pwcs, size_t n) converts the wide-character string pwcs to a multibyte string starting at str. At most n bytes are written to str.
Declaration
Following is the declaration for wcstombs() function.
size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)
Parameters
str − This is the pointer to an array of char elements at least n bytes long.
pwcs − This is wide-character string to be converted.
n − This is the maximum number of bytes to be written to str.
Return Value
This function returns the number of bytes (not characters) converted and written to str, excluding the ending null-character. If an invalid multibyte character is encountered, -1 value is returned.
Example
The following example shows the usage of wcstombs() function.
#include <stdio.h> #include <stdlib.h> #define BUFFER_SIZE 50 int main () { size_t ret; char *MB = (char *)malloc( BUFFER_SIZE ); wchar_t *WC = L"http://www.tutorialspoint.com"; /* converting wide-character string */ ret = wcstombs(MB, WC, BUFFER_SIZE); printf("Characters converted = %u\n", ret); printf("Multibyte character = %s\n\n", MB); return(0); }
Let us compile and run the above program that will produce the following result −
Characters converted = 29 Multibyte character = http://www.tutorialspoint.com