Partager via


strcmp, wcscmp (Windows CE 5.0)

Send Feedback

Developing an Application > Microsoft C Run-time Library for Windows CE > Run-time Library Reference

Compare strings.

int strcmp( const char *string1, const char *string2);int wcscmp( const wchar_t *string1, const wchar_t *string2);

Parameters

  • string1, string2
    Null-terminated strings to compare.

Return Values

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

Remarks

These functions are supported by all versions of the C run-time libraries.

The strcmp function compares string1 and string2 lexicographically and returns a value indicating their relationship. wcscmp is the wide-character version of strcmp. The arguments and return value of wcscmp are wide-character strings. These two functions behave identically otherwise.

The following table shows generic-text routine mappings for this function.

TCHAR.H Routine _UNICODE Defined
_tcscmp wcscmp

For more information about TCHAR.H routines, see Generic Text Mappings.

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.

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.

_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.

Example

Description

The following example compares two strings.

Code

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

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 = strcmp( string1, string2 );
   if( result > 0 )
      strcpy( tmp, "greater than" );
   else if( result < 0 )
      strcpy( tmp, "less than" );
   else
      strcpy( tmp, "equal to" );
   printf( "\tstrcmp:   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 );
}
// Output
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

Requirements

OS Versions: Windows CE 2.0 and later.

Header: stdio.h, string.h.

Link Library: coredll.dll.

See Also

memcmp | _memicmp | strncmp

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.