Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wctomb 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 wctomb function converts a wide character to a multi-byte character. To use the wctomb function, the stdlib.h header file needs to be included in the program as shown below:

#include <stdlib.h>

Prototype

The prototype of the wctomb function is as follows:

int wctomb(char* pointerMb, wchar_t wc);

The function takes two arguments and returns an integer value. It stores the return value at the memory location pointed by pointerMb and stores a maximum of MB_CUR_MAX characters.

Return value

It returns different values based on whether the pointerMb is Null or not. The return values are represented in the figure below:

Figure 1: Different return values of wtcomb under different conditions

Example 1

The following code explains the use of the wtcomb function when pointerMb is NULL:

#include <stdio.h>
#include <stdlib.h>
int main () {
wchar_t wc_valid = L'x';
char *pointerMbNull = NULL;
int returnValue;
returnValue = wctomb( pointerMbNull, wc_valid );
printf("PointerMb: %.1s\n", pointerMbNull);
printf("Return Value: %u\n", returnValue);
return(0);
}

Example 2

The following code explains the use of the wtcomb function when pointerMb is not NULL:

#include <stdio.h>
#include <stdlib.h>
int main () {
wchar_t wc_valid = L'x';
char *pointerMb = (char *)malloc(sizeof( char ));
int returnValue;
returnValue = wctomb( pointerMb, wc_valid );
printf("PointerMb: %.1s\n", pointerMb);
printf("Return Value: %u\n", returnValue);
return(0);
}

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