Why are there multiple C functions for case-insensitive comparison
Asked Answered
G

4

5

For comparing any strings without considering their case, there are various C library functions such as strcasecmp(), stricmp() and stricmpi(). What is the difference between these?

Geisha answered 26/5, 2014 at 13:35 Comment(3)
See also: https://mcmap.net/q/1903001/-is-the-function-strcmpi-in-the-c-standard-libary-of-iso/2372604Quentinquercetin
Note the comment by R.. GitHub STOP HELPING ICE on the linked question: “Note that "case insensitive comparison" is not a simple matter. Do you want one that behaves the same way in all locales, which is what you would need for parsing HTML or something like that? Or do you want one that works for the current locale (possibly mismatching the actual input text language) or that tries to work for all natural languages all at once? Do you want it to take into effect collating rules? Etc..”Scenery
However, if we are only talking of conventional null-terminated C strings in ASCII, the issue scarcely arises, I think — or do some locales sort the basic Latin alphabet differently? If you have to deal with Unicode, then a library such as the ICU - International Components for Unicode may be the answer.Scenery
D
8

Neither stricmp() or strcmpi() are described by the C99 Standard (or POSIX.1-2008).

The Standard way to compare strings in a case insensitive way is to convert both to lowercase (or uppercase) before using strcmp().

The POSIX function is strcasecmp().

Despotism answered 26/5, 2014 at 13:46 Comment(2)
C 2011 also has no case-insensitive comparison functionsScenery
And (draft) C 2023 also has no case-insensitive comparison functions.Despotism
W
8

There are multiple ways to do many things primarily because the standards process lags behind implementations. People see the need for a function (in this case, case insensitive string comparison) and some compiler writers/library writers implement a function called strcmpi, while another group implements stricmp, while another group decides it is not necessary to implement it, while another group implements strcasecmp while another group implements strcmpnocase, etc. Years later, representatives from the various groups meet in mortal combat and the winner's implementation becomes part of the language. Meanwhile, the other implementations continue with the other-named methods and the language grows stronger/fragments/gains bloat (depending on your point of view).

Waltman answered 26/5, 2014 at 13:48 Comment(1)
This is amusing background, but @pmg’s answer, though having fewer votes, is considerably more informative.Scenery
S
4

Both are non-standard extensions, i.e. not part of "the C language" (which is specified by ISO/IEC 9899).

strcmpi() is mentioned as deprecated on MSDN, erroneously calling it "a POSIX function" and referring to _stdicmp as replacement (which is only marginally better).

stricmp() is mentioned as popular on Wikipedia, but non-standard nevertheless.

You have to understand that any library implementation is at liberty to provide additional functions. Like open() and read(), OpenFile(), AllocMem() etc. etc. -- none of these are "the C language", just implemented in C, working on one or more platforms but not necessarily available on others.

Spitz answered 26/5, 2014 at 13:50 Comment(5)
Just for completeness: Yes, read and open are not part of the C standard, but they are part of the POSIX superset. Calling them non-standard is correct, but using them isn't as "unsafe" or non-portable as some other non-standard functions are (like clone or sys_clone)Tutti
@EliasVanOotegem MS DOS had the functions read() and open(). They probably existed in Unix too. Probably with different specs. Since those operative systems are definitely older than POSIX, these functions already existed before POSIX became ISO standard. POSIX "reserved" many things that already existed in multiple versions. I would definitely not assume that functions are portable or safe just because they are part of POSIX.Mathilda
@EliasVanOotegem: A matter of standpoint. I have several issues with POSIX, the "portability" it promises, and the kind of constraints that "POSIX compliance" puts on the OS API designer. But this is very much OT for this question, or Q&A in general. I stand by the wording of my post.Spitz
@lundin: I didn't mean to proclaim POSIX guaranteeing absolute portability (it doesn't), I merely wanted to dot the i's and cross the t's in saying these functions should be available on POSIX compliant systemsTutti
@EliasVanOotegem: I would sure hope they are available on POSIX compliant systems, or else the system wouldn't be compliant, would it? ;-)Spitz
M
0
int strcmpi(const char *s1, const char *s2);
int stricmp(const char *s1, const char *s2);

‏strcmpi (implemented as a macro that calls stricmp) performs an unsigned comparison of s1 to s2, without case sensitivity. strcmpi performs an unsigned comparison of s1 to s2, without case sensitivity.

To use strcmpi, you must include STRING.H. This macro is provided for compatibility with other C compilers.

Mateo answered 26/5, 2014 at 14:12 Comment(1)
What do you mean by “unsigned”?Scenery

© 2022 - 2024 — McMap. All rights reserved.