Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wcrtomb() in C ?

Sheza Naveed

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The wcrtomb function converts a wide character to its narrow multi byte character. The wchar.h header needs to be included to use this function as shown below:

#include <wchar.h>

Prototype

The function is written as shown below:

size_t wcrtomb( char *mbc, wchar_t wc, mbstate_t *state);

Parameters

mbc - the pointer to the character array that will store the multi byte character.

wc - the wide-character that is to be converted

state - the pointer to the mbstate_t object

Return value

Figure 1: Different return values of the wcrtomb function

Example

The following code represents the use of the wcrtomb function. Line 13 resets the state to the initial conversion state:

#include <string.h>
#include <stdio.h>
#include <wchar.h>
int main( void )
{
size_t size = 0;
mbstate_t state;
char mbc = 0;
wchar_t* wc = L"Q";
//resetting initial conversion state
memset(&state, 0, sizeof(state));
size = wcrtomb(&mbc, *wc, &state);
if (size > 0)
{
printf("The corrsponding wide character \"");
wprintf(L"%s\"", wc);
printf(" was converted to \"%c\" ", mbc);
printf("multibyte character.\n");
}
else //error condition
{
printf("No corresponding multibyte character "
"was found.\n");
}
}

RELATED TAGS

c

CONTRIBUTOR

Sheza Naveed
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring