mbsinit() Function in C/C++
Last Updated :
22 Jun, 2021
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 :
CPP
#include <bits/stdc++.h>
using namespace std;
void checkfor( mbstate_t ps)
{
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" ;
}
int main()
{
setlocale (LC_ALL, "en_US.utf8" );
char str[] = "\u00df" ;
mbstate_t ps = mbstate_t ();
checkfor(ps);
mbrlen(str, 1, &ps);
checkfor(ps);
return 0;
}
|
Output:
The conversion state is initial conversion state
The conversion state is not initial conversion state
Program 2 :
CPP
#include <bits/stdc++.h>
using namespace std;
void checkfor( mbstate_t ps)
{
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" ;
}
int main()
{
setlocale (LC_ALL, "en_US.utf8" );
char str[] = "" ;
mbstate_t ps = mbstate_t ();
cout << "After ps is initialized, " ;
checkfor(ps);
mbrlen(str, 0, &ps);
cout << "After performing some task, " ;
checkfor(ps);
return 0;
}
|
Output:
After ps is initialized, the conversion state is initial conversion state
After performing some task, the conversion state is initial conversion state
Share your thoughts in the comments
Please Login to comment...