-
Compare 2 String
Code:
#include <stdio.h>
main()
{
char a[10], b[10];
clrscr();
printf("name1 : ");fgets(a,10,stdin);
printf("name 2 : ");fgets(b,10,stdin);
if(a = b){
printf("same");
}else{
printf("different");
}
getch();
return 0;
}
Even i input different string the output always say first statement / "same" ?
-
You need to compile with warnings on, and it would tell you that:
= is the assignment operator.
== is the comparison operator.
That wouldn't actually fix your main problem however. You cannot compare strings with ==. Instead you need something like strcmp, which is located in string.h.
Quzah.
-