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

# strcmp.3

`strcmp` — 比较字符串

## 名称

`strcmp`, `strncmp`

## 库

Lb libc

## 概要

`#include <string.h>`

`Ft int Fn strcmp const char *s1 const char *s2 Ft int Fn strncmp const char *s1 const char *s2 size_t len`

## 描述

`strcmp` 和 `strncmp` 函数按字典序比较以 NUL 结尾的字符串 `s1` 和 `s2`。

`strncmp` 函数比较不超过 `len` 个字符。由于 `strncmp` 设计用于比较字符串而非二进制数据，出现在 **`\0`** 字符之后的字符不参与比较。

## 返回值

`strcmp` 和 `strncmp` 函数返回一个大于、等于或小于 0 的整数，分别对应字符串 `s1` 大于、等于或小于字符串 `s2`。比较使用无符号字符进行，因此 **`\200`** 大于 **`\0`**。

## 参见

[bcmp(3)](/man/string/bcmp.3.md), [memcmp(3)](/man/string/memcmp.3.md), strcasecmp(3), [strcoll(3)](/man/string/strcoll.3.md), [strxfrm(3)](/man/string/strxfrm.3.md), wcscmp(3)

## 标准

`strcmp` 和 `strncmp` 函数遵循 ISO/IEC 9899:1990 ("ISO C89")。

## 历史

`strcmp` 函数首次出现于 Programmer's Workbench (PWB/UNIX)，并移植到 Version 7 AT\&T UNIX；`strncmp` 首次出现于 Version 7 AT\&T UNIX。


---

# 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/string/strcmp.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.
