mbsinit() Function in C/C++

The mbsinit() is an inbuilt function in C++ which is used to check whether ps(passed as a parameter to this function) points to a mbstate_t object that describes an initial conversion state. This function returns non-zero for any mbstate_t object representing an initial state, or if ps is a null pointer.

The state pointed by ps can be set to the initial state by calling :

// ps points now to a zero-valued object
memset (ps, 0, sizeof(*ps));

Syntax :

int mbsinit( const mbstate_t* ps)

Parameter : The function accepts one parameter as described below:

  • ps :Pointer to an mbstate_t object

Return Value : The function returns two value as follows:

  • 0 if ps is not a null pointer and doesn’t represent the initial conversion state.
  • Nonzero if ps is a null pointer or represents the initial conversion state.

Below programs illustrate the above function:
Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// mbsinit() function
  
#include <bits/stdc++.h>
using namespace std;
  
// function to check if the object describes
// the initial conversion state
void checkfor(mbstate_t ps)
{
    // (mbstate_t) Type that holds the information necessary
    // to maintain the state when converting between
    // sequences of multibyte characters and
    // wide characters (either way).
  
    int func = mbsinit(&ps);
  
    if (func)
        cout<<"The conversion state is initial"
                                 <<" conversion state\n";
  
    else
        cout<<"The conversion state is not initial"
                                    <<" conversion state";
}
  
// Driver code
int main()
{
    setlocale(LC_ALL, "en_US.utf8");
  
    // initializing the string
    char str[] = "\u00df";
  
    // initial state
    mbstate_t ps = mbstate_t();
  
    // check if ps is liknked to
    // initial conversion state
    checkfor(ps);
  
    mbrlen(str, 1, &ps);
  
    // check if, after geting the
    // length of multibyte character
    checkfor(ps);
  
    return 0;
}

chevron_right


Output:

The conversion state is initial conversion state
The conversion state is not initial conversion state

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// mbsinit() function
// with empty string
  
#include <bits/stdc++.h>
using namespace std;
  
// function to chack if
// object describes the initial conversion state
void checkfor(mbstate_t ps)
{
    // (mbstate_t) Type that holds the information necessary
    // to maintain the state when converting between
    // sequences of multibyte characters and
    // wide characters (either way).
  
    int func = mbsinit(&ps);
  
    if (func)
        cout << "the conversion state is initial"
                                <<" conversion state\n";
  
    else
        cout << "the conversion state is not initial"
                                <<" conversion state";
}
  
// Driver code
int main()
{
    setlocale(LC_ALL, "en_US.utf8");
  
    // initializing the string
    char str[] = "";
  
    // initial state
    mbstate_t ps = mbstate_t();
  
    // check if ps is liknked to
    // initial conversion state
    cout << "After ps is initialized, ";
    checkfor(ps);
  
    mbrlen(str, 0, &ps);
  
    // check if, after geting the
    // length of multibyte character
    cout << "After performing some task, ";
    checkfor(ps);
  
    return 0;
}

chevron_right


Output:

After ps is initialized, the conversion state is initial conversion state
After performing some task, the conversion state is initial conversion state


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.