能判断两个字符数组相等吗?
时间: 2023-09-30 20:12:30 浏览: 244
判断输入的两个字符串是否相等
5星 · 资源好评率100%
可以判断两个字符数组相等,需要使用比较函数strcmp(),该函数会比较两个字符串的内容,如果相等则返回0。示例代码如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "World";
if (strcmp(str1, str2) == 0) {
printf("str1 is equal to str2\n");
} else {
printf("str1 is not equal to str2\n");
}
if (strcmp(str1, str3) == 0) {
printf("str1 is equal to str3\n");
} else {
printf("str1 is not equal to str3\n");
}
return 0;
}
```
输出结果为:
```
str1 is equal to str2
str1 is not equal to str3
```
阅读全文