> 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/wcstombs.3.md).

# wcstombs.3

`wcstombs` — 将宽字符串转换为字符串

## 名称

`wcstombs`

## 库

Lb libc

## 概要

`#include <stdlib.h>`

`Ft size_t Fo wcstombs char * restrict mbstring const wchar_t * restrict wcstring size_t nbytes Fc`

## 描述

`wcstombs` 函数从初始转换状态开始，将宽字符串 `wcstring` 转换为多字节字符串 `mbstring`。最多将 `nbytes` 字节存储到 `mbstring` 中。字符串末尾的不完整多字节字符不会被存储。若仍有空间，多字节字符串以 NUL 结尾。

## 返回值

`wcstombs` 函数若成功则返回转换的字节数（不含末尾的 NUL 字符），否则返回 (`size_t`)-1。

## 错误

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

**\[Er** EILSEQ] 遇到无效的宽字符。

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

## 参见

[mbstowcs(3)](/man/locale/mbstowcs.3.md), [multibyte(3)](/man/locale/multibyte.3.md), [wcsrtombs(3)](/man/locale/wcsrtombs.3.md), [wctomb(3)](/man/locale/wctomb.3.md)

## 标准

`wcstombs` 函数遵循 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/wcstombs.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.
