# 5.11. gethostname()－我是誰？

比 getpeername() 更简单的函数是 gethostname()，它会返回你运行程序的电脑名，这个名称之後可以用在 gethostbyname()，用来定义你本地端电脑的 IP address。\
\
有什麽更有趣的吗？\
\
我可以想到一些事情，不过这不适合 socket 编程，总之，下面是一段示例：<br>

```c
#include <unistd.h>
int gethostname(char *hostname, size_t size);
```

参数很简单：*hostname* 是指向字符数组（array of chars）的指针，它会储存函数返回的主机名（hostname），而 *size* 是以 byte 为单位的主机名长度。\
\
函数在运行成功时返回 0，在错误时返回 -1，并一样设置 errno。


---

# Agent Instructions: 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:

```
GET https://beej-zhcn.netdpi.net/system_call/gethostnameff0d_wo_shi_shui_ff1f.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
