strcmp, wcscmp, _mbscmp, _mbscmp_l

문자열을 비교합니다.

Important

Windows 런타임에서 실행되는 애플리케이션에서는 _mbscmp_mbscmp_l을 사용할 수는 없습니다. 자세한 내용은 유니버설 Windows 플랫폼 앱에서 지원되지 않는 CRT 함수를 참조하세요.

구문

int strcmp(
   const char *string1,
   const char *string2
);
int wcscmp(
   const wchar_t *string1,
   const wchar_t *string2
);
int _mbscmp(
   const unsigned char *string1,
   const unsigned char *string2
);
int _mbscmp_l(
   const unsigned char *string1,
   const unsigned char *string2,
   _locale_t locale
);

매개 변수

string1, string2
비교할 Null 종료 문자열입니다.

locale
사용할 로캘입니다.

반환 값

이러한 각 함수의 반환 값은 string1에 대한 string2의 서수 관계를 나타냅니다.

string1 관계 string2
< 0 string1string2보다 작은 경우
0 string1string2와 같은 경우
> 0 string1string2보다 큰 경우

매개 변수 유효성 검사 오류 _mbscmp_mbscmp_l 반환_NLSCMPERROR에서 정의 <mbstring.h><string.h> 되는 및 .

설명

strcmp 함수는 string1string2의 서수 비교를 수행하고 해당 관계를 나타내는 값을 반환합니다. wcscmp_mbscmp는 각각 strcmp의 와이드 문자 및 멀티바이트 문자 버전입니다. _mbscmp는 현재 멀티바이트 코드 페이지에 따라 멀티바이트 문자 시퀀스를 인식하며 오류 발생 시 _NLSCMPERROR를 반환합니다. _mbscmp_l 에는 동일한 동작이 있지만 현재 로캘 대신 전달된 로캘 매개 변수를 사용합니다. 자세한 내용은 코드 페이지를 참조 하세요. 또한 null 포인터인 경우 string1 매개 변수 유효성 검사에 설명된 대로 잘못된 매개 변수 처리기를 호출합니다.string2_mbscmp 계속해서 실행하도록 허용한 경우 _mbscmp_mbscmp_l_NLSCMPERROR를 반환하며 errnoEINVAL로 설정됩니다. strcmp 매개 wcscmp 변수의 유효성을 검사하지 않습니다. 그 외의 경우에는 이들 함수가 동일하게 작동합니다.

기본적으로 이 함수의 전역 상태는 애플리케이션으로 범위가 지정됩니다. 이 동작을 변경하려면 CRT의 전역 상태를 참조하세요.

일반 텍스트 루틴 매핑

TCHAR.H 루틴 _UNICODE 정의 _MBCS 되지 않음 _MBCS 정의 _UNICODE 정의
_tcscmp strcmp _mbscmp wcscmp

함수는 strcmp 비교가 서수이며 로캘의 영향을 받지 않는다는 점에서 strcmp 함수와 다릅니다strcoll. strcoll은 현재 로캘의 LC_COLLATE 범주를 사용하여 문자열을 사전 순으로 비교합니다. 범주에 대한 자세한 내용은 다음_wsetlocale을 참조하세요setlocale.LC_COLLATE

"C" 로캘에서 문자 집합(ASCII 문자 집합)의 순서는 사전적 문자 순서와 같습니다. 그러나 다른 로캘에서 문자 집합의 순서는 사전적 순서와 다를 수 있습니다. 예를 들어 특정 유럽 로캘에서 문자 'a'(값 0x61)은 문자 집합의 'ä'(값 0xE4) 앞에 오지만 문자 'ä'는 사전적으로 'a' 문자 앞에 옵니다.

문자 집합과 사전의 문자 순서가 서로 다른 로캘에서는 문자열의 사전 순 비교에 strcoll 대신 strcmp을 사용할 수 있습니다. 원본 문자열에서는 strxfrm을 사용하고 결과 문자열에서는 strcmp를 사용할 수도 있습니다.

strcmp 함수는 대소문자를 구분합니다. _stricmp, _wcsicmp_mbsicmp는 먼저 문자열을 소문자 형식으로 변환한 후에 문자열을 비교합니다. ASCII 테이블([', '', '', '', '', '' 및 '')의 '\\^]_Z'와 '`a' 사이에 있는 문자가 포함된 두 문자열은 대/소문자에 따라 다르게 비교됩니다. 예를 들어 비교가 소문자(abcde"" "")이면 두 문자열 "ABCDE"abcd^> 및 "ABCD^"가 한 쪽을 비교하고 비교가 대문자인 경우 다른 방법("ABCDE" < "ABCD^")을 비교합니다.

요구 사항

루틴에서 반환된 값 필수 헤더
strcmp <string.h>
wcscmp <string.h> 또는 <wchar.h>
_mbscmp <mbstring.h>

호환성에 대한 자세한 내용은 호환성을 참조하세요.

라이브러리

모든 버전의 C 런타임 라이브러리입니다.

예시

// crt_strcmp.c

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown dog jumps over the lazy fox";

int main( void )
{
   char tmp[20];
   int result;

   // Case sensitive
   printf( "Compare strings:\n   %s\n   %s\n\n", string1, string2 );
   result = strcmp( string1, string2 );
   if( result > 0 )
      strcpy_s( tmp, _countof(tmp), "greater than" );
   else if( result < 0 )
      strcpy_s( tmp, _countof (tmp), "less than" );
   else
      strcpy_s( tmp, _countof (tmp), "equal to" );
   printf( "   strcmp:   String 1 is %s string 2\n", tmp );

   // Case insensitive (could use equivalent _stricmp)
   result = _stricmp( string1, string2 );
   if( result > 0 )
      strcpy_s( tmp, _countof (tmp), "greater than" );
   else if( result < 0 )
      strcpy_s( tmp, _countof (tmp), "less than" );
   else
      strcpy_s( tmp, _countof (tmp), "equal to" );
   printf( "   _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

   strcmp:   String 1 is greater than string 2
   _stricmp:  String 1 is equal to string 2

참고 항목

문자열 조작
memcmp, wmemcmp
_memicmp, _memicmp_l
strcoll 함수
_stricmp, _wcsicmp, _mbsicmp, _stricmp_l, _wcsicmp_l, _mbsicmp_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
strspn, wcsspn, _mbsspn, _mbsspn_l
strxfrm, wcsxfrm, _strxfrm_l, _wcsxfrm_l