1. ## if(!strcmp(str1, str2)

Hi!

It looks a bit tricky to me, so please let me know if I understand

this piece of code correctly:

if(!strcmp(str1,str2);

strcmp returns 0 if str1 = str2
strcmp returns 1 if str1 > str2
strcmp returns -1 if str1 < str2

so, if(!strcmp(str1,str2) actually sets strcmp to 1, i.e. to true;

because when strcmp returns 0, meaning that str1 = str2,

if I use "if(strcmp(str1,str2)" I will get "false" as the result

(because strcmp(str1, str2) will equal 0); by negating it

I actually get "true" as a result, and if(!strcmp(str1,str2)

means that both strings are equal. Correct?

When strcmp(str1,str2) returns 1, by negating it I am puting

the condition to false, so if(!strcmp(str1,str2) has the condition

of 0, and thus false. Correct?

If the above is correct, what happens when I get strcmp(str1,str2) = -1?

Thanks a lot! 2. https://www.le.ac.uk/users/rjm1/cotter/page_37.htm
In C true is represented by any numeric value not equal to 0 and false is represented by 0. 3. I don't recommend trying to use the return value of strcmp() as a Boolean value. Do the comparison you actually want, your logic will be clearer.

Code:
```if(strcmp(str1, str2) ==  0)
// Do something.
else if(strcmp(str1, str2) == 1)
// Do something else.
else if(strcmp(str1, str2) == -1)
// Do something else if string 1 is less than str2.```
Remember in C "false" is equal to zero, "true" is everything else.

Jim 4. You seem to have two confusions.

Firstly, if str1 > str2, strcmp returns a positive number, not necessarily 1. If str1 < str2, strcmp returns a negative number, not necessarily -1. If they're equal, strcmp returns 0 like you said.

Secondly, in C, 0 is false and any other number is true. So both 1 and -1 (or -123456, for that matter) are "true" in a boolean context.

Best practice for using strcmp is to compare it to 0 and not use the ! operator:
Code:
```if (strcmp(s1, s2) == 0) {
// they're equal
}

if (strcmp(s1, s2) < 0)) {
// s1 < s2
}``` 5. Originally Posted by algorism You seem to have two confusions.

Firstly, if str1 > str2, strcmp returns a positive number, not necessarily 1. If str1 < str2, strcmp returns a negative number, not necessarily -1. If they're equal, strcmp returns 0 like you said.

Secondly, in C, 0 is false and any other number is true. So both 1 and -1 (or -123456, for that matter) are "true" in a boolean context.

Best practice for using strcmp is to compare it to 0 and not use the ! operator:
Code:
```if (strcmp(s1, s2) == 0) {
// they're equal
}

if (strcmp(s1, s2) < 0)) {
// s1 < s2
}```
Thank you for your answer. I was quoting the code I have seen and the one I have to understand,

that is why I have asked. Do I assume correctly that if str1 = str2, then if(!strcmp(str1, str2) means true? 6. Originally Posted by Ducol
Do I assume correctly that if str1 = str2, then if(!strcmp(str1, str2) means true?
To put it even more plainly, if the strings are equal, then strcmp returns 0, and since !0 == 1, yes, !strcmp(str1, str2) evaluates to true. You already figured that out in your first post. Popular pages Recent additions strcmp 