c16rtomb() function in C/C++

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

Syntax:

size_t c16rtomb(char* s, char16_t c16, mbstate_t* p)

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



  • s: specifies the string where the multibyte character is to be stored.
  • c16: specifies the 16 bit character to convert.
  • p: specifies the pointer to an mbstate_t object used when interpreting the multibyte string.

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

  • On success of the program, the function returns the number of bytes written to the character array pointed to by s.
  • On failure, -1 is returned and EILSEQ is stored in err no.

Below programs illustrates the above function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Ishwar Gupta

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:


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



My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.