Message from C, C++ discussions

December 2019

— Yeah but, strcmp returns 0 when the strings matches but the if works contrary


strcmp returns 0 for equal strings, yes?
then if (strcmp(str1, str2)) {...} else {...} should go to the else block if str1 and str2 are equal.

— Do you experience a different behavior?

— Exactly but isn't it more logically if that worked for the if block? the condition is true so don't execute the else?

Message permanent page

— Not sure what you mean.
You can think of it as adding != 0 to the end.
so if (i) the same as if (i != 0).
And in C++ at least it usually highly recommended to use the second form (if it's not a bool operator call that is intended) as it's more clear.

Message permanent page

— So the condition is true when it's not equal to 0, instead for a function like strcmp it's true when it's equal to 0

Message permanent page

— Don't know, I find it a bit confusing

— Thank you Pavel

— No, it is the same for strcmp

— It true when it's not zero, and false if it is zero

— If the strings matches (true) it will return 0

— Why true?
strcmp doesn't return true or false, it returns int