strcmp
时间: 2023-08-28 12:08:47 浏览: 45
strcmp是C语言中的字符串比较函数,用于比较两个字符串是否相等。它的函数原型为:
```
int strcmp(const char *s1, const char *s2);
```
其中,s1和s2是两个要比较的字符串。如果s1和s2相等,返回值为0;如果s1小于s2,返回值小于0;如果s1大于s2,返回值大于0。
例如,下面的代码演示了如何使用strcmp比较两个字符串:
```
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "hello";
char str2[] = "world";
char str3[] = "hello";
int result1 = strcmp(str1, str2);
int result2 = strcmp(str1, str3);
printf("result1 = %d\n", result1);
printf("result2 = %d\n", result2);
return 0;
}
```
上述代码输出结果为:
```
result1 = -15
result2 = 0
```
因为"hello"小于"world",所以result1返回-15;"hello"等于"hello",所以result2返回0。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)