gpt4 book ai didi

c - strcmp() 认为字符串不相等……但它们是相等的吗?

转载 作者:太空狗 更新时间:2023-10-29 14:50:59 32 4
gpt4 key购买 nike

<分区>

由于未知原因,运行我的 C 程序的结果非常出乎意料。我认为这一定是初学者的错误,但我真的找不到它在哪里。

#include <stdio.h>
#include <string.h>
int main()
{
char string1[50];
char string2[50];
int compare;

puts("Enter two strings: ");
fgets(string1, strlen(string1)+1, stdin);
fgets(string2, strlen(string2)+1, stdin);

compare=strcmp(string1, string2); /* usage of extra variable makes the code more readable but wastes more memory */

printf("%d: ",compare);

if (compare<0) puts("First string is lesser");
else if (compare>0) puts ("First string is bigger");
else puts("Strings are equal");


return 0;
}

关于测试:

Enter two strings: 
heheisntthisalongstring
heheisntthisalongstring
1: First string is bigger


------------------
(program exited with code: 0)
Press return to continue

这些字符串不应该相等吗?

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com