#include <stdio.h>
int main()
{
char a[]=("hello");
char b[10];
printf("enter value: ");
scanf("%s",b);
if(b==a){
printf("%s",a);
}
return 0;
}
when I run this code it shows me to 'enter value' as expected but when I enter 'hello' which is equal to variable 'a' it is not showing the if statement.
CodePudding user response:
== will check that a and b are pointers to the same string in memory, which they aren't. To compare the contents of these strings, you can use strcmp:
if (strcmp(a, b) == 0) {
printf("%s", a);
}
CodePudding user response:
If you want scanf to consume one line of input, write scanf("%s\n",b);.
Another point is that you want to compare buffer contents, instead of pointers. Use strcmp function in the if() clause.
