C Standard Library

C <wchar.h> - wmemcmp() Function



The C <wchar.h> wmemcmp() function is used to compare num wide characters of two blocks of memory pointed to by lhs and rhs lexicographically. The function starts comparing the first pair of wide characters of each block of memory and continues comparing until the pair of wide characters are different in specified number of comparisons.

Syntax

int wmemcmp (const wchar_t* lhs, const wchar_t* rhs, size_t num);

Parameters

lhs Specify pointer to the first block of memory to compare.
ptr2 Specify pointer to the second block of memory to compare.
num Specify number of wide characters to compare.
size_t is an unsigned integer type.

Return Value

Based on the value of lhs and rhs, the function returns the following:

  • Negative value if lhs appears before rhs in lexicographical order.
  • Zero if lhs and rhs compare equal.
  • Positive value if lhs appears after rhs in lexicographical order.

Example: Value returned by function = 0

The example below shows the usage of wmemcmp() function.

#include <stdio.h>
#include <wchar.h>
 
int main (){
  wchar_t str1[20] = L"Hello";
  wchar_t str2[20] = L"Hello";
  
  //comparing first 5 wide characters of 
  //memory blocks str1 and str2
  int retval = wmemcmp(str1, str2, 5);

  //displaying the result
  if(retval != 0)
    printf("First 5 wide characters of str1 and str2 are not equal.\n");
  else
    printf("First 5 wide characters of str1 and str2 are equal.\n");
 
  printf("Value returned by the function: %d\n", retval);

  return 0;
}

The output of the above code will be:

First 5 wide characters of str1 and str2 are equal.
Value returned by the function: 0

Example: Value returned by function < 0

Lets consider this example where the returned value is less than zero.

#include <stdio.h>
#include <wchar.h>
 
int main (){
  wchar_t str1[20] = L"Hello";
  wchar_t str2[20] = L"World";
  
  //comparing first 3 wide characters of 
  //memory blocks str1 and str2
  int retval = wmemcmp(str1, str2, 3);

  //displaying the result
  if(retval != 0)
    printf("First 3 wide characters of str1 and str2 are not equal.\n");
  else
    printf("First 3 wide characters of str1 and str2 are equal.\n");

  printf("Value returned by the function: %d\n", retval);

  return 0;
}

The output of the above code will be:

First 3 wide characters of str1 and str2 are not equal.
Value returned by the function: -1

Example: Value returned by function > 0

Lets consider another example where the returned value is greater than zero.

#include <stdio.h>
#include <wchar.h>
 
int main (){
  wchar_t str1[20] = L"Hello";
  wchar_t str2[20] = L"Apple";
  
  //comparing first 3 wide characters of 
  //memory blocks str1 and str2
  int retval = wmemcmp(str1, str2, 3);

  //displaying the result
  if(retval != 0)
    printf("First 3 wide characters of str1 and str2 are not equal.\n");
  else
    printf("First 3 wide characters of str1 and str2 are equal.\n");

  printf("Value returned by the function: %d\n", retval);

  return 0;
}

The output of the above code will be:

First 3 wide characters of str1 and str2 are not equal.
Value returned by the function: 1

❮ C <wchar.h> Library