if(!strcmp.....

Mar 19, 2012 at 3:29pm
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.
Mar 19, 2012 at 3:34pm
- 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.
Mar 19, 2012 at 3:41pm
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 Mar 19, 2012 at 3:43pm
Mar 19, 2012 at 3:55pm
- 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.
Mar 19, 2012 at 3:56pm
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?
Mar 19, 2012 at 4:05pm
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).

Mar 19, 2012 at 4:23pm
Think of strcmp not as "are these the same?" but as "what are the differences between these two?".
Mar 19, 2012 at 4:30pm
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!
Mar 19, 2012 at 4:32pm
Now, one more question.

Why would you use the syntax

if(!strcmp(str1,str2)


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

or vice versa?

Mar 19, 2012 at 4:37pm
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 Mar 19, 2012 at 4:38pm
Mar 19, 2012 at 4:51pm
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 :)
Mar 19, 2012 at 4:59pm
you mean the first one is two characters shorter... it would be more if you actually care about spacing your operators.
Mar 19, 2012 at 5:06pm
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.
Mar 19, 2012 at 5:30pm
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.