strcmp
时间: 2023-08-28 21:09:46 浏览: 44
strcmp 是 C 语言中的一个字符串比较函数,用于比较两个字符串是否相等。其函数原型为:
```c
int strcmp(const char *s1, const char *s2);
```
其中,s1 和 s2 是要比较的两个字符串,函数返回值为整型,表示比较结果。如果 s1 和 s2 相等,返回值为 0;如果 s1 比 s2 小,返回值为小于 0 的整数;如果 s1 比 s2 大,返回值为大于 0 的整数。
例如,下面的代码使用 strcmp 函数比较两个字符串是否相等:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "hello";
char str2[20] = "world";
char str3[20] = "hello";
if (strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal\n");
} else {
printf("str1 and str2 are not equal\n");
}
if (strcmp(str1, str3) == 0) {
printf("str1 and str3 are equal\n");
} else {
printf("str1 and str3 are not equal\n");
}
return 0;
}
```
输出结果为:
```
str1 and str2 are not equal
str1 and str3 are equal
```
相关推荐
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)