Difference of stricmp and _stricmp in Visual Studio?

允我心安 提交于 2019-12-05 16:23:44

问题


I may asking a stupid question, but I really can't find an answer with google plus I am still a beginner of using MSVS.

I recently need to use functions to make comparison of two strings. What I don't understand is the difference of stricmp and _stricmp. They both can be used to compare strings and return the same results. I went to check them:

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 );
}

result shows they are the same:

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

I am wondering why...


回答1:


stricmp is a POSIX function, and not a standard C90 function. To avoid name clashes Microsoft deprecated the non-conforming name (stricmp) and recommends using _stricmp instead. There is no difference in functionality (stricmp is merely an alias for _stricmp.)




回答2:


For many library functions, including all the <string.h> functions, the underscore prefixed versions are/were Microsoft's idea of something. I don't recall exactly what.

The non-underscored version is highly portable. Code which uses _stricmp(), _strcpy(), etc. must be handled somehow—edit, #defined, etc.—if the code will be processed by another compiler.



来源:https://stackoverflow.com/questions/12413585/difference-of-stricmp-and-stricmp-in-visual-studio

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