Hacker News new | ask | show | jobs
by bpineau 3679 days ago
It's just a shorthand for '== 0' (since !0 is true, and !<non-zero> is false). strcmp(3) returns 0 when the two strings are identical. So "if (!strcmp(a, b))" means "if the strings a and b are the same.
1 comments

He understands that, but he does not like to use this shorthand for strcmp, because it conveys a conflicting meaning. I'm much the same, and I prefer explicitly writing "== 0" in those situations.
I'm not sure I see it as conflicting meaning versus how C treats boolean expressions. Logical, I want to know if the strings are not equal and that uses boolean syntax to tell me that as opposed to comparing to zero which doesn't convey the meaning of the statement as fast.
It's an artifact of implementation. In some zany world, strcmp could return EQUAL_ENUM. What you are ideally expressing is that you want to know that both strings are equal as determined by strcmp, not that the strcmp return value is nonzero. Now it just so happens that EQUAL_ENUM=0 in this world, so a side effect is that !strcmp(...) will still work. And we also have plenty of examples using this shorthand that it doesn't seem unnatural to a lot of devs. (Doesn't make it any less boots-on-head strange... when in Rome, do as the Romans do...)
I'm not saying its not screwed up, and I'm sure some enums would be interesting, but they are using it in a logical context so I feel like the ! makes a fair amount of sense.

I do wonder what would have happened if truth had been defined like it is in Forth[1]? That would make me go with the enums.

1) False is 0 (all bits set to 0), and True is -1 (all bits set to 1 giving a twos-complement -1).