use of strcmp in code, compile->test->fails, debug, realize I forgot 0==strcmp

I just have some kind of fundamental perception problem with strcmp, stricmp, compare, etc. I KNOW what they do and I know WHY they do it, but when I'm checking equality I just naturally write "if ( strcmp() )..." It's backwards logic to me and it just doesn't occur to me (until later when my code block doesn't work) to write "if ( 0==strcmp() )...". I do this all the time and it slows down my development. Anyone got a tip to reverse my brain on this?

It's like I needed the function to be named "strdoesntcmp"... (yes I know that it couldn't be used for less- or greater-than that way...). Sure I could use #define or inline to make such a thing but that's a hack... I just want to reverse my natural thinking on it if that makes sense...


Just think, that comparison is, in fact, subbtraction (the microprocessor instructions are also working in such way). So strcmp might be called strsub, and in such way zero means that the strings are equal.

Or implement the strcmp specification on your own. While you work it out, the return values will glue to your mind like 1x1.

You can think also, that once you have posted to stackOverflow that you are always forgetting (0==strcmp) and got a lot of confusing answers to it. Once you have this remembered, your mind could not mistake anymore that the compare operator is not the equality operator.

int streq( char const* s1, char const* s2)
    return 0 == strcmp(s1, s2);

inline it if the performance overhead bothers you.

The best answer is to forget strcmp entirely.

std::string s1 = "one";
std::string s2 = "two";
if (s1 == s2) // ...

Edit: If you were familiar with assembly the strcmp function wouldn't confuse you at all. Many processors have a CMP instruction that do a subtraction of two values without storing the result, it just sets the condition flags that tell you if the result was negative, zero, or positive. strcmp just simulates the same thing with strings.

Need Your Help

Form field value doesnt get sent

ruby-on-rails forms request

So I have a RoR form using simple_form gem, and the strange thing is, if I put a string in a field which has integer type in the table, it doesnt even get sent, it just sends an empty string even i...

WebView showing images from local cache


I have to show some HTML content in WebView which has both images from URL's over the internet and images from the local phone cache. The src tag of the img has values like the following:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.