
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
mbrtowc() function in C/C++
This mbrtowc() function is used to convert multibyte sequence to wide character string. This returns the length of the multibyte characters in byte. The syntax is like below.
mbrtowc (wchar_t* wc, const char* s, size_t max, mbstate_t* ps)
The arguments are −
- wc is the pointer which points where the resulting wide character will be stored.
- s is the pointer to multibyte character string as input
- max is the maximum number of bytes in s, that can be examined
- ps is pointing to the conversion state, when interpreting multibyte string.
Example
#include <bits/stdc++.h> using namespace std; void display(const char* s) { mbstate_t ps = mbstate_t(); // initial state int s_len = strlen(s); const char* n = s + s_len; int len; wchar_t wide_char; while ((len = mbrtowc(&wide_char, s, n - s, &ps)) > 0) { wcout << "The following " << len << " bytes are for the character " << wide_char << '\n'; s += len; } } main() { setlocale(LC_ALL, "en_US.utf8"); const char* str = u8"z\u00cf\u7c38\U00000915"; display(str); }
Output
The following 1 bytes are for the character z The following 2 bytes are for the character Ï The following 3 bytes are for the character 簸 The following 3 bytes are for the character क
- Related Articles
- mbrtowc() function in C/C++ program
- iswblank() function in C/C++
- iswpunct() function in C/C++
- strchr() function in C/C++
- strtod() function in C/C++
- memmove() function in C/C++
- memcpy() function in C/C++
- atexit() function in C/C++
- raise() function in C/C++
- mbrlen() function in C/C++
- strftime() function in C/C++
- iswlower() function in C/C++
- towupper() function in C/C++
- iswdigit() function in C/C++
- ldexp() function in C/C++

Advertisements