strcmp怎么用的
时间: 2024-05-28 14:05:11 浏览: 9
strcmp()函数是C语言中的一个字符串比较函数,用于比较两个字符串是否相等。它的使用方法如下:
```c
#include <string.h>
int strcmp(const char *str1, const char *str2);
```
其中,str1和str2是要比较的两个字符串,函数返回值为0表示两个字符串相等,返回值大于0表示str1大于str2,返回值小于0表示str1小于str2。
下面是一个使用strcmp()函数比较两个字符串的例子:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Hello";
char str2[20] = "World";
int result = strcmp(str1, str2);
if (result == 0)
{
printf("The two strings are equal.\n");
}
else if (result > 0)
{
printf("The first string is greater than the second.\n");
}
else
{
printf("The first string is less than the second.\n");
}
return 0;
}
```
输出结果为:
```
The first string is less than the second.
```