if (0 == strcmp(val1,val2)) or (strcmp(val1,val2)  == 0)


if(! strcmp(val1,val2))

Any studies or style guides that support one these styles over the other?

  • 2
    The first style (where the operands are reversed) is a holdover from those days when compilers weren't sophisticated enough to warn you about using an = instead of an ==. As to checking for zero, every c programmer already understands that you don't have to. – Robert Harvey Mar 9 '17 at 16:32
  • So part of my question was going to be that I find the second style harder to read vs == 0 in either direction. So I was wondering if its just a me readability thing or if there is some general guidance which I couldn't find. – Travis Mar 9 '17 at 16:34
  • Nobody uses the first style anymore, unless they're deeply ingrained in the past. – Robert Harvey Mar 9 '17 at 16:37
  • 1
    Every popular language has a consensus style and you should follow it, even if it feels unnatural to you. Simplest is usually best, especially in C. The language and library was explicitly designed so we can write "if (! strcmp(...))" so that's what you should write. – kevin cline Mar 9 '17 at 17:13
  • 5
    Since you've included the C++ tag, I'll mention that for it, the proper course of action is usually "none of the above". Use std::string, so this is if (val1 != val2). – Jerry Coffin Mar 9 '17 at 18:17

My preference is strcmp(x, y) == 0. The reason for this is that this style uses the same operator that would be naturally used in the expression x == y (that just compares pointers for equality and thus doesn't do what you want).

Also, you can do inequality comparisons as well: if you want to check whether x < y, you can do it by strcmp(x, y) < 0. The operator is the same.

Now, the only reason for 0 == strcmp(x, y) is that some old compilers may not warn if you use the = operator inside an if statement. The problem with this way is that if you want to check x < y, you do it by 0 > strcmp(x, y) so the operator just switched to the opposite operator. Not at all intuitive. Because modern compilers warn about "if (x = 0)" there is no reason to use these so-called "Yoda conditions".

Also, !strcmp(x,y) looks like "x not equal with y", which is not what it does. So, I would recommend not to use the ! operator with strcmp().

| improve this answer | |
  • 1
    "Also, !strcmp(x,y) looks like "x not equal with y", which is not what it does. So, I would recommend not to use the ! operator with strcmp()" this is how I kinda feel too. But I wasn't sure if it was something I should try to push for my teams style or not. – Travis Mar 10 '17 at 17:14

One mnemonic I find useful is "not means naught" whenever I am converting an integer into a bool.

As to the unnaturalness, unfortunately you have to get over it, because it is an example of idiomatic usage of the language.

If you are allowed to define utility functions, you can wrap the whole thing into new functions like bool Equals(...) and bool EqualsIgnoreCase(...), which would make a C#-turns-C++ programmer happy.

However, unless your entire team agrees, and your project's stakeholders also accept the consequence that the code base doesn't conform to C++ idioms, borrowing heavily from the idioms of a different language is ill-advised. It prevents the stakeholders from choosing to open-source the project, or making it difficult to recruit more experienced C++ developers into the project.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.