Comparing C-Strings

Do not use the relational operators (<, ==, etc.) to compare C-strings. Instead, use the library function strcmp(), which compares s1 and s2 lexicographically and returns an integer indicating their relationship:

To use strcmp() correctly:

Here's a quick example. The C-strings s1 and s2 are initialized elsewhere. Since we don't need to modify either argument, we can use "pointer-style" C-strings.

const char *s1 = ..., *s2 = ...;

int result = strcmp(s1, s2);

if (result == 0) ...            // equal
else if (result < 0) ...        // s1 < s2
else ...                        // s1 > s2