memcmp() — 버퍼 비교

형식

#include <string.h>
int memcmp(const void *buf1, const void *buf2, size_t count);

언어 레벨

ANSI

스레드세이프

설명

memcmp() 함수는 buf1buf2의 첫 번째 count 바이트를 비교합니다.

리턴값

memcmp() 함수는 다음과 같이 두 개의 버퍼 사이의 관계를 보여주는 값을 리턴합니다.
의미
0보다 작음 buf1buf2보다 작음
0 buf1buf2와 같음
0보다 큼 buf1buf2보다 큼

이 예는 main()에 전달된 첫 번째와 두 번째 인수를 비교하여 큰 인수를 판별합니다.
#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] );
   }
}
 
/****************  If the program is passed the arguments  **************
*****************        firststring and secondstring,     ************
*****************        output should be:                 ************
 
When the first 11 characters are compared,
"firststring" is less than "secondstring"
**********************************************************************/