0
#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.

user438383
  • 5,716
  • 8
  • 28
  • 43

2 Answers2

1

== 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);
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

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.

arrowd
  • 33,231
  • 8
  • 79
  • 110