C++ mbsinit()

The mbsinit() function in C++ determines whether an mbstate_t object describes the initial conversion state.

The mbsinit() function is defined in <cwchar> header file.

mbsinit() prototype

int mbsinit( const mbstate_t* ps);

If ps is not a null pointer, the mbsinit() function checks if ps represents the initial conversion state.


mbsinit() Parameters

  • ps: Pointer to an mbstate_t object to inspect.

mbsinit() Return value

The mbsinit() function returns:

  • 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.

Example: How mbsinit() function works?

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

void test_ps(mbstate_t ps)
{
	if (mbsinit(&ps))
	cout << "the conversion state is initial conversion state" << endl;
	else
	cout << "the conversion state is not initial conversion state" << endl;
}

int main()
{
	setlocale(LC_ALL, "en_US.utf8");
	
	char str[] = "\u00b5";
	mbstate_t ps = mbstate_t();
	
	cout << "Initially after creating ps, ";
	test_ps(ps);
	mbrlen(str, 1, &ps);
	
	cout << "After performing some task, ";
	test_ps(ps);
	
	return 0;
}

When you run the program, the output will be:

Initially after creating ps, the conversion state is initial conversion state
After performing some task, the conversion state is not initial conversion state
Did you find this article helpful?