-2

for some reason when I compare two exact strings, it returns a number other than zero. I am not sure why is it doing this.

Shown here:

printf("%s %s %d\n",target,temp->variableName,strcmp(temp->variableName, target));

Result:"B B 10" <- when I compare B with B it returns 10 even though it should return 0.

Could someone tell me how come it is doing this and what can I do to solve this?

stark
  • 12,615
  • 3
  • 33
  • 50
www.com
  • 1
  • 4
  • 6
    Show [mcve], please. – Eugene Sh. Dec 07 '20 at 14:49
  • 2
    Some implementations just return the difference between the first 2 characters found that to not match. If you get 10 it seems that one of the buffers contains a `LF` (`0x0A`) – Gerhardh Dec 07 '20 at 14:52
  • 3
    My crystal ball says please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221). – Weather Vane Dec 07 '20 at 14:58
  • 4
    I suggest changing the format string to `"[%s] [%s] [%d]\n"` – Weather Vane Dec 07 '20 at 15:02
  • 1
    There are two options here. Either your `strcmp` has a bug (VERY unlikely) or you have a problem in code you have not shown. So please [edit] your question and provide a [mre] – klutt Dec 07 '20 at 15:10

1 Answers1

1

The return value of strcmp is the ASCII value of the first delta (this is dependent on implementation, but most decent implementations will behave in this way).

ASCII of 10 is equal to newline. You most likely have a carriage return in one of the strings.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • 2
    How is this downvoted? – Woodstock Dec 07 '20 at 14:52
  • 2
    Maybe you should not write that first sentence in that way. It can be done like this but the standard doesn't specify this. – Gerhardh Dec 07 '20 at 14:53
  • 2
    Hm... while the first sentence is correct for any sane implementations, there is no guarantee that `strcmp` returns any ascii value of anything. _Any_ positive, zero or _any_ negative value is required. It's common to be confused when `strcmp` is inlined by the compiler and returns -1 instead of the difference, ex. like [here](https://stackoverflow.com/questions/16454144/why-the-returns-of-strcmp-is-different) and many other SO questions. You shouldn't depend on that. – KamilCuk Dec 07 '20 at 14:55
  • 1
    @Woodstock I guess another reason for DV is that you're guessing before OP has corrected the question with a mre. And KamilCuk is correct. You should fix your answer. Nothing wrong with mentioning that many implementations solves it this way, but there's no guarantee. – klutt Dec 07 '20 at 14:57
  • 1
    if there was a new line the out put would have printed a new line, but output does not show new line, one of the `B` would have printed in next line – IrAM Dec 07 '20 at 15:13
  • Not the downvoter - but fwiw, editing the first sentence to acknowledge the problem is _likely because the implementation of `strcmp` used_ ... (add your own words) – ryyker Dec 07 '20 at 15:18
  • Not the downvoter – IrAM Dec 07 '20 at 15:19
  • @IrAM Very good point there – klutt Dec 07 '20 at 15:25
  • @ryyker Except that it isn't very likely according ti IrAM's analysis. And since OP does not want to fix the question with a [mre] I have close voted the question. – klutt Dec 07 '20 at 15:26
  • 1
    done thanks for thoughts – Woodstock Dec 07 '20 at 15:27
  • @klutt - I do not dispute your inclination, however I know that sometimes I am called away from my desk in the middle of a conversation in this forum at the most in-opportune times. It may be that this person will come back and fix. – ryyker Dec 07 '20 at 15:29
  • @ryyker Could be the case. Then you're just unlucky. :) – klutt Dec 07 '20 at 15:31
  • 1
    Thanks for re-enforcing my assumption about being busy for awhile :) – ryyker Dec 07 '20 at 15:31
  • @klutt - on the contrary - In this current world condition, I consider myself very fortunate that I have work at all, and that it sometimes does require my attention :) – ryyker Dec 07 '20 at 15:34
  • 1
    @ryyker Haha, true. And getting your question closed because of a phone call is not exactly the end of the world. :D – klutt Dec 07 '20 at 15:37