Visual Studio中stricmp和_stricmp的区别

Difference of stricmp and _stricmp in Visual Studio?

本文关键字:stricmp 区别 Visual Studio      更新时间:2023-10-16

我可能会问一个愚蠢的问题,但我真的找不到谷歌的答案,而且我还是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:nt%snt%snn", 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 2n", 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 2n", tmp );
}

结果显示它们是相同的:

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

我想知道为什么。。。

stricmp是POSIX函数,而不是标准的C90函数。为了避免名称冲突,Microsoft不赞成使用不一致的名称(stricmp),并建议改用_stricmp。功能上没有区别(stricmp只是_stricmp的别名。)

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

非下划线版本具有很强的可移植性。必须以某种方式处理使用_stricmp()_strcpy()等的代码—edit、#defined等;如果代码将由另一个编译器处理。