strcmp
|
定义于头文件
<string.h>
|
||
|
int
strcmp
(
const
char
*
lhs,
const
char
*
rhs
)
;
|
||
按字典序比较两个以空字符结尾的字节字符串。
结果的符号取决于字符串比较中第一对不同字符(均解释为 unsigned char )数值之差的符号。
如果 lhs 或 rhs 不是指向以空字符结尾的字节字符串的指针,则行为未定义。
目录 |
参数
| lhs, rhs | - | 指向要比较的空终止字节字符串的指针 |
返回值
若 lhs 在字典序中出现在 rhs 之前,则返回负值。
当 lhs 与 rhs 比较相等时返回零。
正值表示 lhs 在字典序中出现在 rhs 之后。
注释
此函数与 strcoll 和 strxfrm 不同,不涉及区域设置相关处理。
示例
#include <stdio.h> #include <string.h> void demo(const char* lhs, const char* rhs) { const int rc = strcmp(lhs, rhs); const char* rel = rc < 0 ? "precedes" : rc > 0 ? "follows" : "equals"; printf("[%s] %s [%s]\n", lhs, rel, rhs); } int main(void) { const char* string = "Hello World!"; demo(string, "Hello!"); demo(string, "Hello"); demo(string, "Hello there"); demo("Hello, everybody!" + 12, "Hello, somebody!" + 11); }
输出:
[Hello World!] precedes [Hello!] [Hello World!] follows [Hello] [Hello World!] precedes [Hello there] [body!] equals [body!]
参考文献
- C23 标准 (ISO/IEC 9899:2024):
-
- 7.24.4.2 strcmp 函数 (p: TBD)
- C17 标准 (ISO/IEC 9899:2018):
-
- 7.24.4.2 strcmp 函数 (p: 待定)
- C11 标准 (ISO/IEC 9899:2011):
-
- 7.24.4.2 strcmp 函数 (p: 365-366)
- C99标准(ISO/IEC 9899:1999):
-
- 7.21.4.2 strcmp函数(页码:328-329)
- C89/C90 标准 (ISO/IEC 9899:1990):
-
- 4.11.4.2 strcmp 函数
参见
|
比较两个字符串的前N个字符
(函数) |
|
|
(C95)
|
比较两个宽字符串
(函数) |
|
比较两个缓冲区
(函数) |
|
|
根据当前区域设置比较两个字符串
(函数) |
|
|
C++ documentation
for
strcmp
|
|