Skip to main content
added 19 characters in body
Source Link

The return value of strcmp does not have boolean semantics, as you seem to incorrectly assume.

strcmp is a tri-state comparator, which returns negative, zero or positive value. For equal strings it returns zero, which means that an equality comparison with strcmp should normally look as follows

if (strcmp(str1, str2) == 0) Serial.println("Equals");
else Serial.println("Different");

Yourwhile your original code actually does the opposite. 

There's a moderately widespread ugly habit of writing such comparisons as

if (!strcmp(str1, str2)) Serial.println("Equals");
else Serial.println("Different");

(note the application of ! operator). It works properly, but it has poor readability. Avoid it. For tri-state comparators is a much better idea to spell out the comparison with 0 explicitly.

And you made exactly the same mistake with memcmp. memcmp is also a tri-state comparator, so it should be

if (memcmp(str1, str2, sizeof str2) == 0) Serial.println("Equals");
else Serial.println("Different");

The same applies to strcoll.

P.S. And in fact such zero-terminated strings represented by char[] arrays are called C-style strings, not C++-style strings.

The return value of strcmp does not have boolean semantics, as you seem to incorrectly assume.

strcmp is a tri-state comparator, which returns negative, zero or positive value. For equal strings it returns zero, which means that an equality comparison with strcmp should normally look as follows

if (strcmp(str1, str2) == 0) Serial.println("Equals");
else Serial.println("Different");

Your code actually does the opposite. There's a moderately widespread ugly habit of writing such comparisons as

if (!strcmp(str1, str2)) Serial.println("Equals");
else Serial.println("Different");

(note the application of ! operator). It works properly, but it has poor readability. Avoid it. For tri-state comparators is a much better idea to spell out the comparison with 0 explicitly.

And you made exactly the same mistake with memcmp. memcmp is also a tri-state comparator, so it should be

if (memcmp(str1, str2, sizeof str2) == 0) Serial.println("Equals");
else Serial.println("Different");

P.S. And in fact such strings are called C-style strings, not C++-style strings.

The return value of strcmp does not have boolean semantics, as you seem to incorrectly assume.

strcmp is a tri-state comparator, which returns negative, zero or positive value. For equal strings it returns zero, which means that an equality comparison with strcmp should normally look as follows

if (strcmp(str1, str2) == 0) Serial.println("Equals");
else Serial.println("Different");

while your original code actually does the opposite. 

There's a moderately widespread ugly habit of writing such comparisons as

if (!strcmp(str1, str2)) Serial.println("Equals");
else Serial.println("Different");

(note the application of ! operator). It works properly, but it has poor readability. Avoid it. For tri-state comparators is a much better idea to spell out the comparison with 0 explicitly.

And you made exactly the same mistake with memcmp. memcmp is also a tri-state comparator, so it should be

if (memcmp(str1, str2, sizeof str2) == 0) Serial.println("Equals");
else Serial.println("Different");

The same applies to strcoll.

P.S. And in fact such zero-terminated strings represented by char[] arrays are called C-style strings, not C++-style strings.

Source Link

The return value of strcmp does not have boolean semantics, as you seem to incorrectly assume.

strcmp is a tri-state comparator, which returns negative, zero or positive value. For equal strings it returns zero, which means that an equality comparison with strcmp should normally look as follows

if (strcmp(str1, str2) == 0) Serial.println("Equals");
else Serial.println("Different");

Your code actually does the opposite. There's a moderately widespread ugly habit of writing such comparisons as

if (!strcmp(str1, str2)) Serial.println("Equals");
else Serial.println("Different");

(note the application of ! operator). It works properly, but it has poor readability. Avoid it. For tri-state comparators is a much better idea to spell out the comparison with 0 explicitly.

And you made exactly the same mistake with memcmp. memcmp is also a tri-state comparator, so it should be

if (memcmp(str1, str2, sizeof str2) == 0) Serial.println("Equals");
else Serial.println("Different");

P.S. And in fact such strings are called C-style strings, not C++-style strings.