1

So I have this (not the full code)

char list[1000][10];

strcpy(list[0],"ab");

printf("%d\n",strcmp(list[0],"ab"));

and the strcmp is returning 0. Can somebody explain why it's doing so?

Thanks in advance.

Hasan Saad
  • 279
  • 4
  • 11
  • 4
    Because the two compared strings are equal, so `strcmp` returns `0`. – Daniel Fischer Aug 23 '13 at 21:46
  • http://linux.die.net/man/3/strcmp, http://pubs.opengroup.org/onlinepubs/9699919799/functions/strcmp.html, or just `man strcmp` if your system has the `man` command. – Keith Thompson Aug 23 '13 at 21:47
  • See [`strcmp()`](http://en.cppreference.com/w/c/string/byte/strcmp) (and I advise drilling up a few levels and spending some time on that entire site for awhile). – WhozCraig Aug 23 '13 at 21:49
  • @WhozCraig Rather [this](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strcmp.html). We are in C, not in C++. –  Aug 23 '13 at 21:50
  • 2
    I see you're also on the Mathematics board. Keep in mind, if you're used to writing stuff in Matlab, that its `strcmp()` behaves differently (it returns 1 for equality whereas C's `strcmp()` returns 0). – FatalError Aug 23 '13 at 21:51
  • 2
    Here is an interesting idea - read the manual page? Might get a better understanding and answer quicker – Ed Heal Aug 23 '13 at 21:57
  • @H2CO3 Um. did you check that link before correcting me? Look again. its the C-subsection of the side, not `std::strcmp()`. Sok the more sights he has to see the better IMHO. – WhozCraig Aug 23 '13 at 21:59
  • @WhozCraig Correct... it's just that the `cppreference.com` URL misled me. :( –  Aug 23 '13 at 22:02
  • @H2CO3 No worries man. Totally understandable. Just didn't want you thinking I'd lost my marbles more than I already have =P. – WhozCraig Aug 23 '13 at 22:03

6 Answers6

6

The strcmp method will return 0 if list[0] contains "ab" in this case.

It returns:

Returns an integral value indicating the relationship between the strings:

A zero value indicates that both strings are equal.

A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • oh. I thought functions usually return 1 when they're true. This was a stupid mistake of mine. Sorry. – Hasan Saad Aug 23 '13 at 21:48
  • 1
    @HasanSaad: It depends on the function. `strcmp` doesn't return a true or false value. Never attempt to use any function until you've read the documentation for that function. – Keith Thompson Aug 23 '13 at 21:51
  • @HasanSaad "I thought" - don't do that. **Don't make assumptions. *Never.*** Read the documentation. –  Aug 23 '13 at 21:51
  • @HasanSaad: `strcmp` doesn’t return “true” or “false”; it returns a comparison result (negative if a < b, zero if a = b, positive if a > b). – Stephen Canon Aug 23 '13 at 21:51
  • @HasanSaad That's true in some cases, but every function can be different. `strcmp` is used to compare, so it returns postive/zero/negative. – Reed Copsey Aug 23 '13 at 21:51
0

strmp returns 0 when the strings match. Unless I am missing something, it's behaving as expected.

see: http://www.tutorialspoint.com/ansi_c/c_strcmp.htm

AdamSpurgin
  • 951
  • 2
  • 8
  • 28
0

strcmp() does an ordinal comparison of the strings, not an equality test. The return value of 0 means the strings are equal!

If you want to test for equality use this pattern:

if (strcmp(s, "ab") == 0) {
    // strings are equal
}
Alex MDC
  • 2,416
  • 1
  • 19
  • 17
0

Because strcpy() is a function that copies the "right" string to the "left" string. So, after strcpy(list[0],"ab");, the content of list[0] is "ab". Then, they are equal strings, and strcmp returns 0, which means "equal".

pablo1977
  • 4,281
  • 1
  • 15
  • 41
0

Your String is matched that is why it is returning 0 do like this...

    char list[1000][10];

    strcpy(list[0],"ab");
    if(strcmp(list[0],"ab")==0)
         printf("Matched\n",);
spt025
  • 2,134
  • 2
  • 20
  • 26
0

As mentioned by Daniel, the returnd value is 0.

Taken from cplusplus.com

Returns an integral value indicating the relationship between the strings: A zero value indicates that the characters compared in both strings form the same string. A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

You should also work with strncmp rather than with strcmp.

pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • `strncmp` is not always better than `strcmp`. As long as you know both arguments are valid strings, and you want to compare the entire strings and not just an initial substring, `strcmp` is fine. – Keith Thompson Aug 23 '13 at 21:50
  • No cplusplus.com, please. Firstly, the question is tagged C. Second, cplusplus.com is a horrible site with utter inaccuracies. [this](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strcmp.html) is better. –  Aug 23 '13 at 21:50
  • It may be, that cplusplus.com is a horrible site and mostly about C++. But in this case the answer seems to be fine and the reference is also about C. And good point by Keith, he is right. – pzaenger Aug 23 '13 at 21:53