Why are there multiple C functions for case-insensitive comparison

混江龙づ霸主 提交于 2021-02-16 14:18:17

问题


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?


回答1:


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).




回答2:


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().




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/23871403/why-are-there-multiple-c-functions-for-case-insensitive-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!