12

私は愚かな質問をするかもしれませんが、私は本当にグーグルで答えを見つけることができません、そして私はまだMSVSを使う初心者です。

最近、関数を使用して2つの文字列を比較する必要があります。私が理解していないのは、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 );
}

結果は、それらが同じであることを示しています。

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

なぜだろうか...

4

2 に答える 2

11

stricmpはPOSIX関数であり、標準のC90関数ではありません。名前の衝突を避けるために、Microsoftは非準拠の名前(stricmp)を非推奨にし、_stricmp代わりに使用することをお勧めします。機能に違いはありません(stricmp単なるエイリアス_stricmpです)。

于 2012-09-13T20:40:09.640 に答える
5

すべての関数を含む多くのライブラリ関数の場合<string.h>、アンダースコアの接頭辞付きバージョンは、Microsoftのアイデアです。正確には思い出せません。

強調されていないバージョンは、移植性が高いです。_stricmp()、などを使用するコードは、コードが別のコンパイラで処理される場合は、_strcpy()何らかの方法(編集、など)で処理する必要があります。#defined

于 2012-09-13T20:35:05.030 に答える