c语言strcmp()的用法
时间: 2024-09-23 16:11:38 浏览: 98
C语言中的strcmp()函数用于比较两个字符串是否相等。该函数的原型如下:
```
int strcmp(const char *s1, const char *s2);
```
其中,s1和s2分别是要比较的两个字符串。
如果s1与s2相等,则该函数返回0;如果s1小于s2,则返回一个负数;如果s1大于s2,则返回一个正数。
需要注意的是,strcmp()函数比较的是字符串的ASCII码值,而不是字符串本身的长度。
以下是一个简单的例子:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello";
char str2[] = "World";
if(strcmp(str1, str2) == 0)
{
printf("str1和str2相等\n");
}
else if(strcmp(str1, str2) < 0)
{
printf("str1小于str2\n");
}
else
{
printf("str1大于str2\n");
}
return 0;
}
```
输出结果为:
```
str1小于str2
```
相关问题
C语言 strcmp用法
C语言中的strcmp函数用于比较两个字符串的大小。它的函数原型如下所示:
```c
int strcmp(const char* str1, const char* str2);
```
该函数接受两个参数,分别是要比较的两个字符串str1和str2。函数返回一个整数值,表示两个字符串的大小关系。
- 如果str1小于str2,则返回一个负数。
- 如果str1等于str2,则返回0。
- 如果str1大于str2,则返回一个正数。
下面是一个使用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 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;
}
```
运行上述代码,输出结果为"str1 is less than str2",因为字符串"hello"在字典序中小于字符串"world"。
c语言strcmp用法
`strcmp`是C语言中的一个字符串比较函数,用于比较两个字符串是否相等。其函数原型为:
```c
int strcmp(const char* str1, const char* str2);
```
其中`str1`和`str2`分别为要比较的两个字符串。函数返回值为整型,如果`str1`小于`str2`,则返回一个负整数;如果`str1`等于`str2`,则返回0;如果`str1`大于`str2`,则返回一个正整数。
下面是两个使用`strcmp`函数的例子:
引用:
```c
int my_strcmp(const char* str1, const char* str2) {
assert(str1 && str2);
while (*str1 == *str2) {
if (*str1 == '\0') {
return 0;
}
str1++;
str2++;
}
return(*str1 - *str2);
}
int main() {
int ret1 = my_strcmp("abcd", "abcde");
if (ret1 < 0) {
printf("<\n");
} else if (ret1 == 0) {
printf("==\n");
} else {
printf(">\n");
}
return 0;
}
```
引用:
```c
int main() {
char arr1[20] = "zhangsan";
char arr2[] = "zhangsanfeng";
int ret = strcmp(arr1, arr2);
if (ret < 0) {
printf("<\n");
} else if (ret == 0) {
printf("==\n");
} else {
printf(">\n");
}
return 0;
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)