Visual Studio中stricmp和_stricmp的区别?

For*_*Cpp 11 c++ case-sensitive case-insensitive string-comparison

我可能会问一个愚蠢的问题,但我真的找不到谷歌的答案加上我仍然是使用MSVS的初学者.

我最近需要使用函数来比较两个字符串.我不明白的是stricmp和_stricmp的区别.它们都可用于比较字符串并返回相同的结果.我去检查一下:

char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown dog jumps over the lazy fox";

void main( void )
{
   char tmp[20];
   int result;
   /* Case sensitive */
   printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );
   result = stricmp( string1, string2 );
   if( result > 0 )
      strcpy( tmp, "greater than" );
   else if( result < 0 )
      strcpy( tmp, "less than" );
   else
      strcpy( tmp, "equal to" );
   printf( "\tstricmp:   String 1 is %s string 2\n", tmp );
   /* Case insensitive */
   result = _stricmp( string1, string2 );
   if( result > 0 )
      strcpy( tmp, "greater than" );
   else if( result < 0 )
      strcpy( tmp, "less than" );
   else
      strcpy( tmp, "equal to" );
   printf( "\t_stricmp:  String 1 is %s string 2\n", tmp );
}
Run Code Online (Sandbox Code Playgroud)

结果显示它们是相同的:

Compare strings:
    The quick brown dog jumps over the lazy fox
    The QUICK brown dog jumps over the lazy fox

    stricmp:   String 1 is equal to string 2
    _stricmp:  String 1 is equal to string 2
Run Code Online (Sandbox Code Playgroud)

我想知道为什么......

Sta*_*lot 10

stricmp是POSIX函数,而不是标准的C90函数.为了避免名称冲突,Microsoft弃用了不合格的名称(stricmp)并建议使用_stricmp.功能没有区别(stricmp仅仅是别名_stricmp.)

  • `stricmp`不是**POSIX函数,它是Win32函数.POSIX定义了[`strcasecmp`](http://pubs.opengroup.org/onlinepubs/009695399/functions/strcasecmp.html). (7认同)
  • 前导下划线表示它是特定于供应商的符号,不应与任何用户符号或未来标准冲突. (2认同)
  • Microsoft声明它们在文档中是POSIX函数.例如,请参阅:http://msdn.microsoft.com/en-us/library/ms235365(v = vs.80).aspx (2认同)

wal*_*lyk 5

对于许多库函数,包括所有<string.h>函数,下划线前缀版本是/是微软的想法。具体是什么我不记得了。

非下划线版本是高度便携的。使用_stricmp()_strcpy()等的代码必须以某种方式处理——编辑#defined、 等——如果代码将由另一个编译器处理。

  • 然而,`stricmp` 和 `_stricmp` 都不是标准 C 或 C++ 的一部分;从技术上讲,`stricmp` 不是 `&lt;string.h&gt;` 函数之一。但这是一个非常常见的扩展。 (3认同)