strcmp 是 C 语言中用于比较两个字符串的函数。它返回一个整数值:-1(第一个字符串小于第二个字符串)、0(相等)或 1(第一个字符串大于第二个字符串)。

strcmp 的含义
strcmp 是 C 语言中一个函数,用于比较两个字符串。
详细说明
strcmp 函数接收两个参数,第一个参数是字符串的指针,第二个参数也是字符串的指针。函数比较两个字符串的内容,并返回一个整数值:
立即学习“C语言免费学习笔记(深入)”;
- 如果第一个字符串小于第二个字符串,返回 -1。
- 如果第一个字符串等于第二个字符串,返回 0。
- 如果第一个字符串大于第二个字符串,返回 1。
用法
以下是如何使用 strcmp 函数的示例:
<code class="c">#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result < 0) {
printf("str1 is less than str2\n");
} else if (result == 0) {
printf("str1 is equal to str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}</code>输出:
<code>str1 is less than str2</code>











