编辑:更改标题以反映帖子中的两种方法。
我正在尝试比较c语言中的两个字符串,如下所示,但由于某种原因,它总是打印两个字符串不相等
#include <stdio.h>
#include <string.h>
int main()
{
/* A nice long string */
char test[30]="hellow world";
char test2[30];
// to copy string from first array to second array
strcpy(test2, test);
/* now comparing two stering*/
if(strcmp(test2, test))
printf("strigs are equal ");
else
printf("not equal \n");
printf("value of first string is %s and second string is %s",test,test2);
printf("length of string1 is %zu and other string is %zu ",strlen(test2),strlen(test2));
}
我总是得到输出
not equal
value of first string is hellow world and second string is hellow worldlength of string1 is 12 and other string is 12