Функция mbtowc в C
Опубликовано: 12 Апреля, 2022
Преобразование многобайтовой последовательности в широкий символ. Многобайтовый символ, на который указывает pmb, преобразуется в значение типа wchar_t и сохраняется в месте, указанном pwc. Функция возвращает длину многобайтового символа в байтах.
mbtowc имеет собственное внутреннее состояние сдвига, которое изменяется по мере необходимости только при вызове этой функции. Вызов функции с нулевым указателем, поскольку pmb сбрасывает состояние (и возвращает, зависят ли многобайтовые символы от состояния).
Поведение этой функции зависит от категории LC_CTYPE выбранной локали C (библиотека локализации C).
Syntax:
pwc: Pointer to an object of type wchar_t. Alternatively, this argument can be a null pointer, in which case the function does not store the wchar_t translation, but still returns the length in bytes of the multibyte character. pmb: Pointer to the first byte of a multibyte character. Alternatively, this argument can be a null pointer, in which case the function resets its internal shift state to the initial value and returns whether multibyte characters have a state-dependent encoding. max: Maximum number of bytes of pmb to consider for the multibyte character. Return Value: If the argument passed as pmb is not a null pointer, the size in bytes of the multibyte character pointed by pmb is returned when it forms a valid multibyte character and is not the terminating null character. If it is the terminating null character, the function returns zero, and in the case they do not form a valid multibyte character, -1 is returned. If the argument passed as pmb is a null pointer, the function returns a nonzero value if multibyte character encodings are state-dependent, and zero otherwise.
// C program to illustrate mbtowc // function #include <stdio.h> #include <stdlib.h> // function containing mbtowc & wchar_t(C) function void mbtowc_func( const char * pt, size_t max) { int length; // this is a typedef of an integral type wchar_t dest; // reset mbtowc mbtowc (NULL, NULL, 0); while (max > 0) { length = mbtowc (&dest, pt, max); if (length < 1) { break ; } // printing each character in square braces printf ( "[%lc]" , dest); pt += length; max -= length; } } int main() { const char str[] = "geeks portal" ; mbtowc_func(str, sizeof (str)); return 0; } |
Выход:
[g] [e] [e] [k] [s] [] [p] [o] [r] [t] [a] [l] »
Хотите узнать о лучших видео и практических задачах, ознакомьтесь с Базовым курсом C для базового и продвинутого C.