4 ms·
The advice in the article is too broad because there are times when the comparison is not meant in the truthy/falsy sense. For example, strcmp and friends retu
by marcoperaza 7y ago
The advice in the article is too broad because there are times when the comparison is not meant in the truthy/falsy sense. For example, strcmp and friends return 0 if the strings are equal, less than 0 if str1 is lexically less than str2, and more than 0 if str1 is lexically greater than str2.
So if(!strcmp(str1,str2)) is a valid way to check for equality, but I think the negation makes it confusing; it is tempting to intuitively read it as checking if the strings are not equal. if(strcmp(str1,str2) == 0) is the better choice.
- a1369209993 7y agoCalling it (eg) strdiff instead would help here, if(!strdiff(str1,str2)) matches the intuitive interpretation.
- saagarjha 7y agoIt's a three way comparison, not an equality one. If you treat it like that then the return value makes more sense.
- a1369209993 7y agoSure - thats why it shouldn't be str_ne or similar. strDIFF (or rather, strcmp) returns the direction in which strings DIFFer - less than (negative), greater than (positive), or equal (zero).