可以使用strcmp函数比较两个char类型的元素,该函数会把两个char类型的元素逐个字符比较,直到出现不同字符为止或者其中一个char*类型的元素结束为止。使用该函数需要包含头文件
#include
int main() { char str1[] = "Hello"; char str2[] = "Hellp";
if (strcmp(str1, str2) == 0) {
printf("str1 equals str2\n");
} else {
printf("str1 is not equal to str2\n");
}
return 0;
}
输出结果为“str1 is not equal to str2”。