Comparing two like strings returns not equal

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sravanyadav51
    New Member
    • Sep 2011
    • 1

    Comparing two like strings returns not equal

    Why are output:s1 and s2 not equal?

    Code:
    main()
    {
    char *s1="Hello";
    char *s2="Hello";
    if(s1==s2)
      printf("s1 and s2 are equal");
    else
      printf("s1 and s2 are not equal");
    }
    Last edited by Niheel; Sep 30 '11, 11:12 AM.
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    You're comparing pointer values, not their contents. Try printing the hex value of s1 and s2 and you'll see they have different addresses.

    To compare the value of the strings you have to use strcmp or something similar (or use C++ std::string)

    Comment

    • deepduttamvjce
      New Member
      • Oct 2011
      • 2

      #3
      you should try the strcmp() function to compare strings and by that you can exactly compare strings in string.h header file.

      Comment

      Working...