Open In App

c32rtomb() function in C/C++

The c32rtomb() is a built-in function in C/C++ which converts 32 bit character representation to a narrow multibyte character representation. It is defined within the uchar.h header file of C++.

Syntax:



size_t c32rtomb(char* s, char32_t c32, mbstate_t* p)

Parameters: The function accepts three mandatory parameters as shown below:

Return Value: The function returns two values as shown below:



Program 1:




// C++ program to illustrate the
// c32rtomb () function on it's success
#include <iostream>
#include <uchar.h>
#include <wchar.h>
using namespace std;
  
int main()
{
    const char32_t str[] = U"GeeksforGeeks";
    char s[50];
    mbstate_t p{};
    size_t length;
    int j = 0;
  
    while (str[j]) {
        // initializing the function
        length = c32rtomb(s, str[j], &p);
        if ((length == 0) || (length > 50))
            break;
  
        for (int i = 0; i < length; ++i)
            cout << s[i];
        ++j;
    }
  
    return 0;
}

Output:
GeeksforGeeks

Program 2:




// C++ program to illustrate the
// c32rtomb () function on it's failure
#include <iostream>
#include <uchar.h>
#include <wchar.h>
using namespace std;
  
int main()
{
    const char32_t str[] = U"";
    char s[50];
    mbstate_t p{};
    size_t length;
    int j = 0;
  
    while (str[j]) {
        // initializing the function
        length = c32rtomb(s, str[j], &p);
        if ((length == 0) || (length > 50))
            break;
  
        for (int i = 0; i < length; ++i)
            cout << s[i];
        ++j;
    }
  
    return 0;
}

Output:

Note: There is no output in the above program since it is a case of failure.


Article Tags :