What is wctomb in C ?

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);
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved