c语言比较两个字符串
时间: 2023-06-21 18:08:14 浏览: 56
在C语言中,比较两个字符串可以使用标准库函数strcmp。
strcmp函数的原型如下:
```c
int strcmp(const char *s1, const char *s2);
```
该函数接受两个参数,分别是要比较的两个字符串s1和s2。函数返回一个整数值,如果s1等于s2,返回0;如果s1小于s2,返回一个负数;如果s1大于s2,返回一个正数。
以下是一个示例程序,演示了如何使用strcmp函数比较两个字符串:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "hello";
char str2[] = "world";
int result = strcmp(str1, str2);
if (result == 0)
{
printf("str1 is equal to str2\n");
}
else if (result < 0)
{
printf("str1 is less than str2\n");
}
else
{
printf("str1 is greater than str2\n");
}
return 0;
}
```
输出结果为:
```
str1 is less than str2
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)