if(!strcmp.....

This is making me crazy. This is the syntax I'm dealing with, and I just can't decide whether this statement results in 'true' or 'false'. I thought I understood this, but if I understand this correctly, then the code is 'wrong', or at least it definitely doesn't make sense to me.

if (!strcmp(str1, str2))


I understand(I think) that strcmp returns 0 if the strings are equal, and I understand(I think) that the ! is 'not' or reversing the result. Is that correct?

- So, if str1 = str2, then the strcmp returns 0, and the ! reverses that and makes it a 1, returning 'false' to the 'if'?

- and, if str1 <> str2, then the strcmp returns 1, and the ! reverses that and makes it be 0, returning 'true' to the 'if'?

- and then, since the if statement executes the subsequent actions only if true, then the subsequent actions would only be executed if str1 <> str2?

My problem is that the subsequent statement only makes sense if it's being executed when str1 = str2.

Help.
- So, if str1 = str2, then the strcmp returns 0, and the ! reverses that and makes it a 1, returning 'false' to the 'if'?

No. 1 is TRUE, not false.
Also strcmp() returns either 0 or an positive integer, depending on if str1 matches str2, or where the first inconstant character is.

!(int) any non-zero integer returns false, and !(0) returns true.
Last edited on
- So, if str1 = str2, then the strcmp returns 0, and the ! reverses that and makes it a 1, returning 'false' to the 'if'?

No. 1 is TRUE, not false.


That confuses me. If the ! were not there, and it just read:
if(strcmp(str1, str2))


Wouldn't that be true? Or, does the 0 actually return a false to the 'if' statement, and a 1 result in a 'true'?

Maybe that's where the root of my confusion lies.

I've been assuming that a 0 = true, because I've also seen the following syntax:

if(strcmp(str1, str2) !=0)
{then do this}


And I thought the action would execute if the strings are not equal.
Also strcmp() returns either 0 or an positive integer, depending on if str1 matches str2, or where the first inconstant character is.


Yes, I understand that, and I think it can return a negative integer if the first mismatched character str2 is greater than str1, right?
OK - I think I understand where my confusion is, if someone would verify this.

I've been thinking "it's true that string1 = string2, and strcmp returns 0 if they are equal, therefore, 0 must mean 'true'"

But, it seems perhaps that the problem is in my basic assumption, and that a 0 actually means 'false' for the other functions (or at least for the 'if).

Think of strcmp not as "are these the same?" but as "what are the differences between these two?".
Think of strcmp not as "are these the same?" but as "what are the differences between these two?".


oh, that makes perfect sense now!

Thank you so much!
Now, one more question.

Why would you use the syntax

if(!strcmp(str1,str2)


instead of
if(strcmp(str1,str2) = 0

or vice versa?

Because you can't assign to the return value of a function if it is not a reference. Perhaps you meant ==? In that case, which is shorter to type?

1
2
if(!strcmp(str1, str2))
if(strcmp(str1, str2)==0)
Last edited on
Because you can't assign to the return value of a function if it is not a reference. Perhaps you meant ==? In that case, which is shorter to type?


yes, you're correct, I did mean ==.


and yes, the second one is 1 character shorter to type :)
you mean the first one is two characters shorter... it would be more if you actually care about spacing your operators.
you mean the first one is two characters shorter... it would be more if you actually care about spacing your operators.


right :p


It's just that the second one seems SO much easier to read and understand. But I guess if you're coding all day long, every 2 characters count.
The first one is obfuscated.
strcmp returns 0 if they are equal, therefore, 0 must mean 'true'"
strcmp)() compares two strings.
0 means equal
>0 means greater
<0 means lesser
That convention is also used in qsort()
Topic archived. No new replies allowed.