1

I have got two strings (string literals in that case), say

char *name        = "Fotis";
char *second_name = "Fotis";

and I have two ways to compare them, which are both giving me an accurate result. The first one is with the equality operator (==), likewise:

if (name == second_name)
    printf ("Great success!\n");

and the second one is through the strcmp function available in string.h:

if (strcmp (name, second_name) == 0)
    printf ("Great success!\n");

My question is: Which one of the two is the most {efficient | idiomatic} way to compare two strings in C? Is there another, more idiomatic way?

NlightNFotis
  • 9,559
  • 5
  • 43
  • 66
  • depends... do you want semantic comparison... or just byte comparison? – Mgetz Feb 06 '14 at 17:51
  • accurate results with the first method? Are you overriding something? – Marco A. Feb 06 '14 at 17:52
  • 1
    I think the first method is giving accurate results because the two variables point to the same string literal. If he `malloc`d memory for these, they would not be equal. – Gavin Feb 06 '14 at 17:55

4 Answers4

8

The second way with strcmp is correct, the first way using == is incorrect because it's only comparing the value of pointers, not the contents of the strings.

The two strings that you compare happen to be both string literals, they may, and may not have the same pointer value by the standard.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 3
    In general, if you have two separate strings, they will be stored at different locations, so the pointers will be different, so the `==` will give 'unequal' even if the strings contain the same sequence of characters. C does not have built-in string comparison; to compare strings, you must use `strcmp()` or a relative. – Jonathan Leffler Feb 06 '14 at 17:53
  • @JonathanLeffler Thanks for that. That helped me clarify the concept in my mind. – NlightNFotis Feb 06 '14 at 17:54
  • A reminder that `strcmp` only compares byte strings, it does not handle any sort of unicode normalization etc. – Mgetz Feb 06 '14 at 18:00
1

The natural way would be to use strcmp since it's more accurate.

mclc
  • 65
  • 10
1

Your first method using == is comparing the pointers, so it will only return true if the two are exactly the same char array, meaning they point to the same piece of memory. The other method using strcmp is the way you would do this, because it is comparing the actual content of the strings. So it can return true even if the two strings are in different locations in memory.

The reason your first method is appearing to work correctly is because the two variables are pointing to the same string literal, so they basically point to the same location in memory when they're the same string, and different locations when they're different strings. If you used malloc to allocate memory for those strings, and set their contents, then they would point to different locations in memory, so your first method would return false and the second would return true for the same string of text.

Gavin
  • 8,204
  • 3
  • 32
  • 42
0
if (name == second_name)
    printf ("Great success!\n");

Watch out: you're comparing pointers equality with this one, not the strings' texts.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • 1
    It's `C`. How can I override the `==` operator? I thought you could only do that in C++ with operator overloading? – NlightNFotis Feb 06 '14 at 17:55
  • 1
    It's C — there is no way to override the `==` operator (or any other operator, come to that). You can only do that in C++. – Jonathan Leffler Feb 06 '14 at 17:56
  • oh you're both right I didn't notice the C tag. I'm editing my answer. – Marco A. Feb 06 '14 at 17:57
  • 1
    most compilers will optimise multiple static strings with the same value. So your name and second_name will point to the same place in memory, because of this when you compare the pointers using == they are the same. This is just a fluke. Having said that if the two pointers point to the same place they must be the same string?! – AnthonyLambert Feb 06 '14 at 17:58
  • You can't assume that for sure plus if the OP used his code somewhere else it wouldn't work and he'd assume the two strings are equal. – Marco A. Feb 06 '14 at 18:08