c语言strcmp代码
时间: 2023-08-12 12:35:15 浏览: 90
下面是一个简单的 C 语言 strcmp() 函数的代码示例:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = "Hello";
char str2[100] = "World";
int result = strcmp(str1, str2);
if(result == 0)
{
printf("两个字符串相同\n");
}
else
{
printf("两个字符串不相同\n");
}
return 0;
}
```
这个程序使用了 `strcmp()` 函数来比较两个字符串,如果两个字符串相同,那么 `strcmp()` 函数会返回 0,否则返回一个非零值。在这个例子中,我们使用 `printf()` 函数输出了结果。
相关问题
c语言strcmp源代码
C语言中的strcmp函数的源代码实现如下:
```c
int strcmp(const char* str1, const char* str2)
{
while(*str1 && (*str1 == *str2))
{
str1++;
str2++;
}
return *(const unsigned char*)str1 - *(const unsigned char*)str2;
}
```
该函数使用了指针遍历字符串的方式,逐个比较两个字符串的字符,直到发现不同的字符或者遍历到字符串的结束位置为止。如果两个字符串完全相同,则返回0;否则,返回两个字符串第一个不同字符的ASCII码值之差。
需要注意的是,由于该函数会修改指针的指向,因此在调用该函数之前需要确保传入的参数指向的字符串是可读的,并且不会被修改。
C语言strcmp函数代码
```c
#include <stdio.h>
#include <string.h>
int main () {
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);
if(ret < 0) {
printf("str1 小于 str2");
} else if(ret > 0) {
printf("str1 大于 str2");
} else {
printf("str1 等于 str2");
}
return(0);
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)