I tried understanding both of them but I did not find any differences except for strcoll()
this reference says that it
compares two null terminated strings according to current locale as defined by the LC_COLLATE category.
On the second thoughts and I know I am asking another question for detailed answer, what exactly is this locale, for both C and C++?
strcmp compares both the strings till null-character of either string comes whereas strncmp compares at most num characters of both strings.
The strcoll() function compares two strings. The comparison of the strings may vary depending on the locale settings (A<a or A>a). Note: The strcoll() is case-sensitive but not binary-safe. Note: If the current locale is C or POSIX, this function works the same way as strcmp().
This function compares the string pointed to by str1 with the one pointed by str2.The strcoll() function performs the comparison based on the rules of the current locale's LC_COLLATE category. Syntax: int strcoll(const char *str1, const char *str2)
The strcmp subroutine performs a case-sensitive comparison of the string pointed to by the String1 parameter and the string pointed to by the String2 parameter, and analyzes the extended ASCII character set values of the characters in each string. The strcmp subroutine compares unsigned char data types.
strcmp()
takes the bytes of the string one by one and compare them as is whatever the bytes are.
strcoll()
takes the bytes, transform them using the locale, then compares the result. The transformation re-orders depending on the language. In French, accentuated letters come after the non-accentuated ones. So é is after e. However, é is before f. strcoll()
gets it right. strcmp()
not so well.
However, in many cases strcmp()
is enough because you don't need to show the result ordered in the language (locale) in use. For example, if you just need to quickly access a large number of data indexed by a string you'd use a map indexed by that string. It probably is totally useless to sort those using strcoll()
which is generally very slow (in comparison to strcmp()
at least.)
For details about characters you may also want to check out the Unicode website.
In regard to the locale, it's the language. By default it is set to "C" (more or less, no locale). Once you select a location the locale is set accordingly. You can also set the LC_LOCALE environment variable. There are actually many such variables. But in general you use predefined functions that automatically take those variables in account and do the right thing for you. (i.e. format dates / time, format numbers / measures, compute upper / lower case, etc.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With