> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/locale/wctomb.3.md).

# wctomb.3

`wctomb` — 将宽字符码转换为字符

## 名称

`wctomb`

## 库

Lb libc

## 概要

`#include <stdlib.h>`

`Ft int Fn wctomb char *mbchar wchar_t wchar`

## 描述

`wctomb` 函数将宽字符 `wchar` 转换为多字节字符，并将结果存储在 `mbchar` 中。`mbchar` 所指向的对象必须足够大以容纳该多字节字符，最多可能需要 `MB_LEN_MAX` 字节。

以空 `mbchar` 指针调用时，若当前 locale 需要移位状态则返回非零值，否则返回零；若需要移位状态，则将移位状态重置为初始状态。

## 返回值

若 `mbchar` 为 `NULL`，当支持移位状态时 `wctomb` 函数返回非零值，否则返回零。若 `mbchar` 有效，`wctomb` 返回 `mbchar` 中处理的字节数，若无法识别或转换任何多字节字符则返回 -1。在这种情况下，`wctomb` 的内部转换状态是未定义的。

## 错误

`wctomb` 函数在以下情况下会失败：

**\[Er** EILSEQ] 检测到无效的多字节序列。

**\[Er** EINVAL] 内部转换状态无效。

## 参见

[mbtowc(3)](/man/locale/mbtowc.3.md), [wcrtomb(3)](/man/locale/wcrtomb.3.md), [wcstombs(3)](/man/locale/wcstombs.3.md), wctob(3)

## 标准

`wctomb` 函数遵循 ISO/IEC 9899:1999 ("ISO C99")。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/locale/wctomb.3.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
