You can use the functions in this section to perform comparisons on the contents of strings and arrays. As well as checking for equality, these functions can also be used as the ordering functions for sorting operations (seeChapter 12 [Searching and Sorting], page 343for an example of this).
Unlike most comparison operations in C, the string comparison functions return a nonzero value if the strings arenot equivalent rather than if they are. The sign of the value indicates the relative ordering of the first characters in the strings that are not equivalent: a negative value indicates that the first string is “less” than the second, while a positive value indicates that the first string is “greater”.
The most common use of these functions is to check only for equality. This is canonically done with an expression like ‘! strcmp (s1, s2)’.
All of these functions are declared in the header file ‘string.h’.
Function
intmemcmp (const void *a1, const void *a2, size_t size)
The function memcmp compares the size bytes of memory beginning at a1
same sign as the difference between the first differing pair of bytes (interpreted asunsigned charobjects, then promoted toint).
If the contents of the two blocks are equal,memcmpreturns0.
Function
intwmemcmp (const wchar_t *a1, const wchar_t *a2, size_tsize)
The function wmemcmp compares the size wide characters beginning at a1
against thesize wide characters beginning ata2. The value returned is smaller than or larger than zero depending on whether the first differing wide character isa1 is smaller or larger than the corresponding character ina2.
If the contents of the two blocks are equal,wmemcmpreturns0.
On arbitrary arrays, thememcmpfunction is mostly useful for testing equality. It usually isn’t meaningful to do byte-wise ordering comparisons on arrays of things other than bytes. For example, a byte-wise comparison on the bytes that make up floating-point numbers isn’t likely to tell you anything about the relationship between the values of the floating-point numbers.
wmemcmpis really only useful to compare arrays of typewchar_t, since the function looks atsizeof (wchar_t)bytes at a time, and this number of bytes is system dependent.
You should also be careful about usingmemcmpto compare objects that can con- tain “holes”, such as the padding inserted into structure objects to enforce alignment requirements, extra space at the end of unions and extra characters at the ends of strings whose length is less than their allocated size. The contents of these “holes” are indeterminate and may cause strange behavior when performing byte-wise com- parisons. For more predictable results, perform an explicit component-wise com- parison.
For example, given a structure-type definition like:
struct foo {
unsigned char tag; union { double f; long i; char *p; } value; };
you are better off writing a specialized comparison function to comparestruct fooobjects instead of comparing them withmemcmp.
Function
intstrcmp (const char *s1, const char *s2)
Thestrcmpfunction compares the strings1 againsts2, returning a value that has the same sign as the difference between the first differing pair of characters (interpreted asunsigned charobjects, then promoted toint).
Chapter 5: String and Array Utilities 107 If the two strings are equal,strcmpreturns0.
A consequence of the ordering used bystrcmpis that ifs1 is an initial sub- string ofs2, thens1 is considered to be “less than”s2.
strcmpdoes not take sorting conventions of the language the strings are writ- ten in into account. To get that one has to usestrcoll.
Function
intwcscmp (const wchar_t *ws1, const wchar_t *ws2)
The wcscmp function compares the wide-character string ws1 against ws2. The value returned is smaller than or larger than zero, depending on whether the first differing wide characterws1 is smaller or larger than the corresponding character inws2.
If the two strings are equal,wcscmpreturns0.
A consequence of the ordering used by wcscmp is that if ws1 is an initial substring ofws2, thenws1 is considered to be “less than”ws2.
wcscmpdoes not take sorting conventions of the language the strings are writ- ten in into account. To get that one has to usewcscoll.
Function
intstrcasecmp (const char *s1, const char *s2)
This function is likestrcmp, except that differences in case are ignored. How uppercase and lowercase characters are related is determined by the currently selected locale. In the standard"C"locale, the characters ¨A and ¨a do not match, but in a locale that regards these characters as parts of the alphabet, they do match.
strcasecmpis derived fromBSD.
Function
intwcscasecmp (const wchar_t *ws1, const wchar_T *ws2)
This function is likewcscmp, except that differences in case are ignored. How uppercase and lowercase characters are related is determined by the currently selected locale. In the standard"C"locale, the characters ¨A and ¨a do not match, but in a locale that regards these characters as parts of the alphabet, they do match.
wcscasecmpis aGNUextension.
Function
intstrncmp (const char *s1, const char *s2, size_t size)
This function is the similar to strcmp, except that no more than size wide characters are compared. In other words, if the two strings are the same in their firstsize wide characters, the return value is zero.
Function
intwcsncmp (const wchar_t *ws1, const wchar_t *ws2, size_tsize)
This function is the similar to wcscmp, except that no more than size wide characters are compared. In other words, if the two strings are the same in their firstsize wide characters, the return value is zero.
Function
intstrncasecmp (const char *s1, const char *s2, size_tn)
This function is likestrncmp, except that differences in case are ignored. Like
strcasecmp, how uppercase and lowercase characters are related is locale dependent.
strncasecmpis aGNUextension.
Function
intwcsncasecmp (const wchar_t *ws1, const wchar_t *s2, size_tn)
This function is likewcsncmp, except that differences in case are ignored. Like
wcscasecmp, how uppercase and lowercase characters are related is locale dependent.
wcsncasecmpis aGNUextension.
Here are some examples showing the use of strcmpandstrncmp(equiva- lent examples can be constructed for the wide character functions). These exam- ples assume the use of the ASCII character set. If some other character set—say,
EBCDIC—is used instead, then the glyphs are associated with different numeric
codes, and the return values and ordering may differ.
strcmp ("hello", "hello")
⇒ 0 /* These two strings are the same. */ strcmp ("hello", "Hello")
⇒ 32 /* Comparisons are case-sensitive. */ strcmp ("hello", "world")
⇒ -15 /* The character’h’comes before’w’. */ strcmp ("hello", "hello, world")
⇒ -44 /* Comparing a null character against a comma. */ strncmp ("hello", "hello, world", 5)
⇒ 0 /* The initial 5 characters are the same. */
strncmp ("hello, world", "hello, stupid world!!!", 5)
⇒ 0 /* The initial 5 characters are the same. */
Function
intstrverscmp (const char *s1, const char *s2)
The strverscmp function compares the string s1 against s2, considering them as holding indices/version numbers. Return value follows the same con- ventions as found in thestrverscmpfunction. In fact, ifs1 ands2 contain no digits,strverscmpbehaves likestrcmp.
Basically, we compare strings normally (character-by-character) until we find a digit in each string. Then we enter a special comparison mode, where each
Chapter 5: String and Array Utilities 109 sequence of digits is taken as a whole. If we reach the end of these two parts without noticing a difference, we return to the standard comparison mode. There are two types of numeric parts: integral andfractional (those begin with a ’0’). The types of the numeric parts affect the way we sort them:
• integral/integral: We compare values as you would expect. • fractional/integral: The fractional part is less than the integral one.
• fractional/fractional: Things become a bit more complex. If the common prefix contains only leading zeros, the longest part is less than the other one; else the comparison behaves normally.
strverscmp ("no digit", "no digit")
⇒ 0 /* same behavior as strcmp. */ strverscmp ("item#99", "item#100")
⇒ <0 /* same prefix, but 99<100. */ strverscmp ("alpha1", "alpha001")
⇒ >0 /* fractional part inferior to integral one. */ strverscmp ("part1_f012", "part1_f01")
⇒ >0 /* two fractional parts. */ strverscmp ("foo.009", "foo.0")
⇒ <0 /* idem, but with leading zeros only. */
This function is especially useful when dealing with file-name sorting, because file names frequently hold indices and version numbers.
strverscmpis aGNUextension.
Function
intbcmp (const void *a1, const void *a2, size_t size)
This is an obsolete alias formemcmp, derived fromBSD.