Partager via


strcmp, wcscmp, _mbscmp

Compare strings.

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

Parameters

  • string1, string2
    Null-terminated strings to compare.

Return Value

The return value for each of these functions indicates the lexicographic relation of string1 to string2*.*

Value

Relationship of string1 to string2

< 0

string1 less than string2

0

string1 identical to string2

> 0

string1 greater than string2

On an error, _mbscmp returns _NLSCMPERROR, which is defined in STRING.H and MBSTRING.H.

Remarks

The strcmp function compares string1 and string2 lexicographically and returns a value indicating their relationship. wcscmp and _mbscmp are, respectively, wide-character and multibyte-character versions of strcmp. _mbscmp recognizes multibyte-character sequences according to the current multibyte code page and returns _NLSCMPERROR on an error. (For more information, see Code Pages.) Also, if string1 or string2 is a null pointer, _mbscmp invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, _mbscmp returns _NLSCMPERROR and sets errno to EINVAL. strcmp and wcscmp do not validate their parameters. These three functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined

_tcscmp

strcmp

_mbscmp

wcscmp

The strcmp functions differ from the strcoll functions in that strcmp comparisons are not affected by locale, whereas the manner of strcoll comparisons is determined by the LC_COLLATE category of the current locale. For more information on the LC_COLLATE category, see setlocale.

In the "C" locale, the order of characters in the character set (ASCII character set) is the same as the lexicographic character order. However, in other locales, the order of characters in the character set may differ from the lexicographic order. For example, in certain European locales, the character 'a' (value 0x61) precedes the character 'ä' (value 0xE4) in the character set, but the character 'ä' precedes the character 'a' lexicographically.

In locales for which the character set and the lexicographic character order differ, use strcoll rather than strcmp for lexicographic comparison of strings according to the LC_COLLATE category setting of the current locale. Thus, to perform a lexicographic comparison of the locale in the above example, use strcoll rather than strcmp. Alternatively, you can use strxfrm on the original strings, then use strcmp on the resulting strings.

_stricmp, _wcsicmp, and _mbsicmp compare strings by first converting them to their lowercase forms. Two strings containing characters located between 'Z' and 'a' in the ASCII table ('[', '\', ']', '^', '_', and '`') compare differently, depending on their case. For example, the two strings "ABCDE" and "ABCD^" compare one way if the comparison is lowercase ("abcde" > "abcd^") and the other way ("ABCDE" < "ABCD^") if the comparison is uppercase.

Requirements

Routine

Required header

strcmp

<string.h>

wcscmp

<string.h> or <wchar.h>

_mbscmp

<mbstring.h>

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// 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

.NET Framework Equivalent

System::String::CompareOrdinal

See Also

Reference

String Manipulation (CRT)

memcmp, wmemcmp

_memicmp, _memicmp_l

strcoll Functions

_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