memcmp() - バイトの比較

標準

標準/拡張機能 C/C++ 依存項目

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification、バージョン 3

両方  

形式

#include <string.h>

int memcmp(const void *buf1, const void *buf2, size_t count);

機能説明

memcmp() 組み込み関数は、buf1buf2 の 先頭 count バイトを比較します。

この関係は、異なる左端の最初のバイトの対の 値における差の符号で判別されます。値は EBCDIC エンコードに依存します。この関数はロケールに依存しません

戻り値

以下のとおり、buf1buf2 の 関係を示します。
意味
< 0
buf1 が指すバッファーの内容 は、buf2 が指すバッファーの内容よりも小さい。
= 0
buf1 が指すバッファーの内容 は、buf2 が指すバッファーの内容と同一である。
> 0
buf1 が指すバッファーの内容 は、buf2 が指すバッファーの内容より大きい。

CELEBM12
⁄* CELEBM12                                      

   This example compares first and second arguments passed to                   
   main to determine which, if either, is greater.                              
                                                                                
 *⁄                                                                             
#include <stdio.h>                                                              
#include <string.h>                                                             
                                                                                
int main(int argc, char ** argv)                                                
{                                                                               
  int  len;                                                                     
  int  result;                                                                  
                                                                                
  if ( argc != 3 )                                                              
  {                                                                             
     printf( "Usage: %s string1 string2¥n", argv[0] );
  }                                                                             
  else                                                                          
  {                                                                             
     ⁄* Determine the length to be used for comparison *⁄                       
     if (strlen( argv[1] ) < strlen( argv[2] ))                                 
       len = strlen( argv[1] );                                                 
     else                                                                       
       len = strlen( argv[2] );                                                 
                                                                                
     result = memcmp( argv[1], argv[2], len );                                  
                                                                                
     printf( "When the first %i characters are compared,¥n", len );
     if ( result == 0 )                                                         
       printf( "¥"%s¥" is identical to ¥"%s¥"¥n", argv[1], argv[2] );
     else if ( result < 0 )                                                     
       printf( "¥"%s¥" is less than ¥"%s¥"¥n", argv[1], argv[2] );
     else                                                                       
       printf( "¥"%s¥" is greater than ¥"%s¥"¥n", argv[1], argv[2] );
  }                                                                             
}                                                                               

出力

プログラムに引数 の firststringsecondstring が 渡されると、以下を取得することができます。
When the first 11 characters are compared,
“firststring” is less than “secondstring”

関連情報