2012-01-05 The C function strcmp() return a useful value, and compared to its name, a good one. But, unfortunately, we usually use this function to compare strings for equality. This leads to such code: if (!strcmp(s, "foo")) { /* s equals "foo" */ } Unfortunately, the necessary negation proposes a different logic on a quick look. We are used to idioms. They enable us to under- stand source code quickly, if (!) it uses these idioms. If some- thing looks familiar, then it should do the expected thing. If something looks strange, then one should take a closer look. Hence, if you do a common task, then do it the common way. If you do something special, then make it look special. However, strcmp() violates the is-truth idiom, if it is used for equality comparison. I believe, that the following form of the same check is more readable: if (strcmp(s, "foo")==0) { /* s equals "foo" */ } I omit the spaces around the equality operator, to suggest this test to be a single unit. Every equality test with strcmp() should have the `==0'. But how to handle inequality? There are two forms coming to my mind: if (!(strcmp(s, "foo")==0)) { /* s equals "foo" */ } if (strcmp(s, "foo")!=0) { /* s equals "foo" */ } The former form would be the logical expolation of the concept ``If you want to negate: prefix a `!'.'' But it is bulky and in- convenient. Omiting the parens keeps the semantics, but would too much resemble the classic form, shown in the first code exam- ple. The latter form requires a distinction between the two operators, thus splitting the idiom in two. Nonetheless, I like it more. In my eyes, the real solution to the problem would be a wrapper function: int streq(const char *s1, const char *s2) { return strcmp(s1, s2)==0; } (The performance addicted can have it as a macro too, of course.) There's always the same question coming up: Get used to the com- mon style or run your own variant, which you believe to be better? It starts with a set of library functions, macros and typedefs, and likely leads to your own style of C that you translate into real C, using a preprocessor. Well, there are standards and there is innovation, and they strive for contrary goals. http://marmaro.de/lue/ markus schnalke